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

.

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
$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);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

   

  • Something like

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    $response = (new ExternalResourceClient(60))->post('https://api.twilio.com/Messages.json', [
    "From" => "whatsapp:+123456789",
    "To" => "whatsapp:+123456799",
    "Body" => "Test"
    ], [
    'Accept' => 'application/json',
    'Content-Type' => 'application/json',
    'Authorization' => 'Basic ' . base64_encode("SDFGHRTY:ERTYUI"),
    ]);
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

  • Hello  

    Thank you for reply.

    I have used the same way but showing error To field is require. That means post array not sending ?

  • Can you use Twilio's PHP Client library?

  • Nice post and please provide more information. Thanks for sharing.

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

    Let me know what i am doing wrong.

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    $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();
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

  • Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    $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"),
    ]);
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

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

  • Thank you for reply.