简体   繁体   中英

Curl POST issue with php

I've got an sms server that works perfectly with a curl request like this one

curl -X POST http://local_IP/raspisms/api/scheduled/ -H 'X-Api-Key: XXXXXXXXXXXXXXXXXXXX' -d 'text=My%20Message' -d 'numbers=06XXXXXXXX'

I succeed in running a php code like this one

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://local_IP/raspisms/api/scheduled/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "text=My%Message&numbers=06XXXXXXXX");

$headers = array();
$headers[] = 'X-Api-Key: XXXXXXXXXXXXXXXX';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

I pass the message variable like this no problem

$text = "My Message";
define('POSTVARS', 'numbers=06XXXXXXXX&text=');  
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://local_IP/raspisms/api/scheduled/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, POSTVARS.$text);

$headers = array();
$headers[] = 'X-Api-Key: XXXXXXXXXXXX';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

I'm stuck to pass both message and phone number variable. I already try the array() way

$data = array("text"=>$messages,
"numbers"=>$tel
);

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

seems correct to me, but doesn't work

I tried too

curl_setopt($ch, CURLOPT_POSTFIELDS, "text={$message}&numbers={$tel}");

I try the variables with double quote {\"text\":\"$message\"}

Also tried this one $url = 'http://api.pushingbox.com/pushingbox?devid=vB9C6311111098CB&sensor='. $delta. '&temperature='. $temp; $url = 'http://api.pushingbox.com/pushingbox?devid=vB9C6311111098CB&sensor='. $delta. '&temperature='. $temp;

I don't know what i can try to solve it (i don't receive any kind of error)

With define('POSTVARS', 'text='.$text.'&numbers='.$tel); i echo POSTVAR to get text=MyMessage&numbers=06XXXXXXXX

It looks like this solution is working but the api "refuses" it

Between curl_setopt($ch, CURLOPT_POSTFIELDS, POSTVARS); which is refused and curl_setopt($ch, CURLOPT_POSTFIELDS, "text=MyMessage&numbers=06XXXXXXXX"); which is sent, what's the difference?

The problem was it cannot handle local phone number (06XXXXXXXX) but with international format no problem (+33), so variable has to start with "%2B33XXXXXXXX" and that works fine

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