Home > Blockchain >  Curl POST issue with php
Curl POST issue with php

Time:02-04

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 Message' -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;

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

CodePudding user response:

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 ?

CodePudding user response:

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

  •  Tags:  
  • Related