SMS API CURL 问题
SMS API issue with CURL
大家好请有人帮我解决短信问题API。这是短信 API 提供商提供的完整代码:
我不知道出了什么问题,也不知道如何解决 415 问题。 API 提供商不提供编码支持
/*
*:: How to generate Token in PHP using our Restful API
*/
"https://restapi.bulksmsonline.com/rest/api/v1/sms/gettoken/username/MYUSERNAME/password/MYPASSWORD",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
/*
*::: How to send sms with your generated Token
*/
"https://restapi.bulksmsonline.com/rest/api/sms/send",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"from\":\"Sender Name\",\"to\":[\"16813000014\",\"16813000014\"],\"type\":\"Text\",\"content\":\"Sample SMS Content To Be Sent\"}",
CURLOPT_HTTPHEADER => array(
"token : USER TOKEN GENERATED FROM GET TOKEN API",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
这是我开发的代码:
$curl=curl_init("https://restapi.bulksmsonline.com/rest/api/v1/sms/gettoken/username/xxxxx/password/xxxxx");
curl_setopt_array($curl,[
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
]);
$response = curl_exec($curl);
$err = curl_error($curl);
$ch = curl_init("https://restapi.bulksmsonline.com/rest/api/v1/sms/send");
$field = array('from'=>'From me', 'to'=>'237668426982', 'type'=>'Text', 'content'=>'message');
$head = array('token'=>'$response', 'content-type'=>'application/json');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING,"" );
curl_setopt($ch, CURLOPT_MAXREDIRS,10 );
curl_setopt($ch, CURLOPT_TIMEOUT,30 );
curl_setopt($ch, CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'POST' );
curl_setopt($ch, CURLOPT_POSTFIELDS,$field );
curl_setopt($ch, CURLOPT_HTTPHEADER,$head );
$output = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $output;
}
这是显示的响应:
{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.13","title":"Unsupported Media Type","status":415,"traceId":"|54541add-41130078fe10574f."}
请帮我解决这个问题。
谢谢
根据 API 提供商的示例更改您的 post 字段和 headers。 post 字段应该是 JSON 字符串,而不是数组。 headers 数组不是关联数组,它是字符串数组。
$headers = ["token: {$response}", "content-type: application/json"];
$fields = ['from' => 'From me', 'to' => ['237668426982'], 'type' => 'Text', 'content' => 'message'];
$postFields = json_encode($fields);
然后将它们用于 cURL 请求,
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
另一方面,在您的代码中,您有这个 'token'=>'$response'
,单引号内的 PHP 变量;这是行不通的。如果变量有一个字符串文字,那么你根本不需要引号。如果必须使用引号,则使用双引号内的变量。
希望对您有所帮助。
大家好请有人帮我解决短信问题API。这是短信 API 提供商提供的完整代码:
我不知道出了什么问题,也不知道如何解决 415 问题。 API 提供商不提供编码支持
/*
*:: How to generate Token in PHP using our Restful API
*/
"https://restapi.bulksmsonline.com/rest/api/v1/sms/gettoken/username/MYUSERNAME/password/MYPASSWORD",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
/*
*::: How to send sms with your generated Token
*/
"https://restapi.bulksmsonline.com/rest/api/sms/send",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"from\":\"Sender Name\",\"to\":[\"16813000014\",\"16813000014\"],\"type\":\"Text\",\"content\":\"Sample SMS Content To Be Sent\"}",
CURLOPT_HTTPHEADER => array(
"token : USER TOKEN GENERATED FROM GET TOKEN API",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
这是我开发的代码:
$curl=curl_init("https://restapi.bulksmsonline.com/rest/api/v1/sms/gettoken/username/xxxxx/password/xxxxx");
curl_setopt_array($curl,[
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
]);
$response = curl_exec($curl);
$err = curl_error($curl);
$ch = curl_init("https://restapi.bulksmsonline.com/rest/api/v1/sms/send");
$field = array('from'=>'From me', 'to'=>'237668426982', 'type'=>'Text', 'content'=>'message');
$head = array('token'=>'$response', 'content-type'=>'application/json');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING,"" );
curl_setopt($ch, CURLOPT_MAXREDIRS,10 );
curl_setopt($ch, CURLOPT_TIMEOUT,30 );
curl_setopt($ch, CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'POST' );
curl_setopt($ch, CURLOPT_POSTFIELDS,$field );
curl_setopt($ch, CURLOPT_HTTPHEADER,$head );
$output = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $output;
}
这是显示的响应:
{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.13","title":"Unsupported Media Type","status":415,"traceId":"|54541add-41130078fe10574f."}
请帮我解决这个问题。 谢谢
根据 API 提供商的示例更改您的 post 字段和 headers。 post 字段应该是 JSON 字符串,而不是数组。 headers 数组不是关联数组,它是字符串数组。
$headers = ["token: {$response}", "content-type: application/json"];
$fields = ['from' => 'From me', 'to' => ['237668426982'], 'type' => 'Text', 'content' => 'message'];
$postFields = json_encode($fields);
然后将它们用于 cURL 请求,
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
另一方面,在您的代码中,您有这个 'token'=>'$response'
,单引号内的 PHP 变量;这是行不通的。如果变量有一个字符串文字,那么你根本不需要引号。如果必须使用引号,则使用双引号内的变量。
希望对您有所帮助。