Reputation: 11
I want to execute curl command with -d
to send SMS using Africa Talking API unfortunately i can't get any response from the server rather in the response body there's false
Please help me how to send the request am new to curl.
Here is the curl sample from https://build.at-labs.io/docs/sms%2Fsending
curl -X POST \
https://api.sandbox.africastalking.com/version1/messaging \
-H 'Accept: application/json' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'apiKey: MyAppApiKey' \
-d 'username=MyAppUsername&to=%2B254711XXXYYY,%2B254733YYYZZZ&message=Hello%20World!&from=myShortCode'
And to my understanding bellow is what i implemented
$messages = array(
'username'=>'sandbox', //rather my username
'to'=>$phone, // 266XXXXXXX,266XXXXXX
'message'=>$text,//Hello
'from'=>$from //Sandbox
);
$url = "https://api.sandbox.africastalking.com/version1/messaging";
//$url="https://api.africastalking.com/version1/messaging";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type:application/x-www-form-urlencoded',
'apiKey:my-api-key'
));
curl_setopt($ch, CURLOPT_POSTFIELDS, ($messages)); //json_encode($messages)
$server_output = curl_exec($ch);
curl_close($ch);
var_dump($server_output);
Output false
Upvotes: 0
Views: 277
Reputation: 21
You need to build a query string in order for you to send the data successfully to the endpoint. That means changing
curl_setopt($ch, CURLOPT_POSTFIELDS, ($messages)); //json_encode($messages)
to
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($messages)); //json_encode($messages)
Upvotes: 1
Reputation: 1
Curl is ok....I think the problem is with AfricasTalking documentation. However this worked for me.
curl -X POST https://api.sandbox.africastalking.com/version1/messaging -H 'Accept: application/json' -H 'Content-Type: application/x-www-form-urlencoded' -H 'apiKey: MyAppApiKey' -d username=MyAppUsername&to=%2B254711XXXYYY,%2B254733YYYZZZ&message=Hello%20World!&from=myShortCode'
if you are not using a sandbox app you should use the live version instead as shown below.
curl -X POST https://api.africastalking.com/version1/messaging -H 'Accept: application/json' -H 'Content-Type: application/x-www-form-urlencoded' -H 'apiKey: MyAppApiKey' -d username=MyAppUsername&to=%2B254711XXXYYY,%2B254733YYYZZZ&message=Hello%20World!&from=myShortCode'
Upvotes: 0