Help with ExternalResourceClient for use CURLOPT_POSTFIELDS & CURLOPT_USERPWD together.

Hello Folks

I am trying to use ExternalResourceClient Instead of PHP Curl.

But not able to get the results. I have gone through the below link and other questions on Sugarclub.

(+) Introducing ExternalResourceClient your friendly Transport Client - Dev Blog - DevClub - SugarClub (sugarcrm.com)

Below is the sample code. As per above link i can be able to use auth but can't send post data. 

Can you please show sample code for UserPwd and PostFields

.

$twiliowhatsapparray = array( "From" => "whatsapp:+123456789", "To" => "whatsapp:+123456799", "Body" => "Test");
            
$twilio_url = "https://api.twilio.com/Messages.json";

$twilio_req = curl_init();
curl_setopt($twilio_req, CURLOPT_URL, $twilio_url);
curl_setopt($twilio_req, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($twilio_req, CURLOPT_POSTFIELDS, $twiliowhatsapparray);
curl_setopt($twilio_req, CURLOPT_USERPWD, "SDFGHRTY:ERTYUI");
$whatsappresult = curl_exec($twilio_req);
curl_close($twilio_req);

   

Parents Reply
  • Hello  
    I am using below code. Above just showed the sample.

    Let me know what i am doing wrong.

    $twiliowhatsapparray = array( "From" => "whatsapp:+".$twilio_from, "To" => "whatsapp:+" . $mobilenumb, "Body" => 'Test');
    
    $twilio_url = "https://api.twilio.com/2010-04-01/Accounts/" . $twilio_accountsid . "/Messages.json";
    $auth     = base64_encode( "{$twilio_accountsid}:{$twilio_authtoken}" );
    
    $response = (new ExternalResourceClient(60))->post($twilio_url, json_encode($twiliowhatsapparray), [
        'Accept' => 'application/json',
        'Content-Type' => 'application/json',
        'Authorization' => 'Basic ' . $auth,
    ]);
                
    $ress =  $response->getBody()->getContents();

Children
  • $accountSid = 'ENTER YOUR IDE';
    $authToken = 'ENTER YOUR TOKEN';
    
    $url = 'https://api.twilio.com/2010-04-01/Accounts/' . $accountSid . '/Messages.json';
    
    $client = new \Sugarcrm\Sugarcrm\Security\HttpClient\ExternalResourceClient(60);
    $response = $client->post($url, [
        'To' => '+TONUMBER',
        'From' => '+FROMNMBER',
        'Body' => 'Hello from Twilio!'
    ], [
        'Accept' => 'application/json',
        'Content-Type' => ' application/x-www-form-urlencoded',
        'Authorization' => 'Basic ' . base64_encode("$accountSid:$authToken"),
    ]);

    I tested this in my sugar. The difference is that I changed the Content-Type