使用 PHP 卷曲 Post
Curl Post using PHP
这是我在 Whosebug 上的第一个 Post。
我需要使用 PHP 对 SMS 网关执行 Curl Post,但我从来没有这样做过,手册上说我应该这样做
curl -u admin:admin -d '{"text":"Hello.","port":[0],"param":[{"number":"123456","text_param":["John"],"user_id":1}]}’ –H "Content-Type:
application/json" http://192.168.1.252/api/send_sms
#I tried some users post, but I can´t get to get it working.
<?php
$ch = curl_init();
$username='admin';
$password='admin';
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_URL,"http://192.168.1.252/api/send_sms");
curl_setopt($ch, CURLOPT_POST, 1);
$data = array(
'text' => "TEXT TO BE SEND",
'port' => 0,
'number'=>"123456",
'text_param'=>"John",
'user_id'=>1
);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
#Receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
$errno = curl_errno($ch);
$error_message = curl_strerror($errno);
echo "cURL error ({$errno}):\n {$error_message}";
curl_close ($ch);
#Further processing ...
if ($server_output == "OK") { echo "OK"; } else { echo "FAILED"; }
?>
解决了,
无论如何,非常感谢。我使用
添加了内容类型
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json'));
并将 $data 重做为肉类规格。
谢谢
这是我在 Whosebug 上的第一个 Post。 我需要使用 PHP 对 SMS 网关执行 Curl Post,但我从来没有这样做过,手册上说我应该这样做
curl -u admin:admin -d '{"text":"Hello.","port":[0],"param":[{"number":"123456","text_param":["John"],"user_id":1}]}’ –H "Content-Type:
application/json" http://192.168.1.252/api/send_sms
#I tried some users post, but I can´t get to get it working.
<?php
$ch = curl_init();
$username='admin';
$password='admin';
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_URL,"http://192.168.1.252/api/send_sms");
curl_setopt($ch, CURLOPT_POST, 1);
$data = array(
'text' => "TEXT TO BE SEND",
'port' => 0,
'number'=>"123456",
'text_param'=>"John",
'user_id'=>1
);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
#Receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
$errno = curl_errno($ch);
$error_message = curl_strerror($errno);
echo "cURL error ({$errno}):\n {$error_message}";
curl_close ($ch);
#Further processing ...
if ($server_output == "OK") { echo "OK"; } else { echo "FAILED"; }
?>
解决了,
无论如何,非常感谢。我使用
添加了内容类型curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json')); 并将 $data 重做为肉类规格。
谢谢