我正在尝试使用OneAPI SMS界面使用cURL从我的应用程序发送SMS消息。
它返回500错误,这是我正在使用的代码:
<?php
$url = "https://oneapi-gw.gsma.com/smssend/2_0/smsmessaging/outbound/tel%3A%2B1234/requests";
$username = "secret";
$password = "secret";
$request = array(
'address' => 'tel%3A%2B1222333444',
'message' => 'hello world',
'senderAddress' => 'tel%3A%2B1234',
'senderName' => 'joe doe'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request));
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
print_r($info);
?>
任何帮助都会得到任何帮助将不胜感激。谢谢!
P.S。
Array ( [url] => https://oneapi-gw.gsma.com/smssend/2_0/smsmessaging/outbound/tel%3A%2B1416XXXYYYY/requests
[content_type] => application/json [http_code] => 500 [header_size] => 494 [request_size] => 349
[filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.333515 [namelookup_time] => 0.000335
[connect_time] => 0.054989 [pretransfer_time] => 0.229044 [size_upload] => 97 [size_download] => 172 [speed_download] => 515
[speed_upload] => 290 [download_content_length] => -1 [upload_content_length] => 97 [starttransfer_time] => 0.333474 [redirect_time] => 0 [certinfo] => Array ( ) )
解决
对于祖先:由于某些原因,cURL无效,所以file_get_contents()是可行的方法。
答案 0 :(得分:0)
如果要在JSON中发布参数值,则不应对值进行URL编码。如果使用'form post'而不是JSON,则必须这样做。
尝试使用命令行curl发出相同的请求 - 您应该能够获得伴随500返回码的服务器错误的详细信息。
答案 1 :(得分:0)
显然,我用cURL解决了这个难题。问题在于它的语法。继承了他们的API的PHP解决方案。
Class SendSMS {
public $phone_number;
public function __construct($phone_number) {
$this->phone_number = $phone_number;
}
public function oneAPI() {
$url = "https://oneapi-gw.gsma.com/smssend/2_0/smsmessaging/outbound/tel%3A7511/requests"; // END POINT
$username = 'secret'; // APP's login
$password = 'secret'; // and psswd
$request = array(
'address' => $this->phone_number,
'message' => 'hello world',
'senderAddress' => 'tel:7511'
);
$request = http_build_query($request);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // hide output
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
}
}
$hello = new SendSMS('+1XXXXXXXXX'); // WHITE LISTED PHONE
$hello->oneAPI();