简体   繁体   中英

Curl Post using PHP

This is mi First Post on Stackoverflow. I need to do a Curl Post to an SMS Gateway with PHP but I´d never done it, the manual says I should do something like this

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"; }
?>

Solved it,

Thanks a lot anyway. I added content type with

curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json')); and redone the $data to meat specs.

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM