简体   繁体   English

Curl POST 问题与 php

[英]Curl POST issue with php

I've got an sms server that works perfectly with a curl request like this one我有一个短信服务器,可以完美地处理像这样的 curl 请求

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我成功运行了像这样的 php 代码

$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我已经尝试了 array() 方式

$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\"}我尝试使用双引号{\"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; $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);define('POSTVARS', 'text='.$text.'&numbers='.$tel); i echo POSTVAR to get text=MyMessage&numbers=06XXXXXXXX我回显 POSTVAR 以获取text=MyMessage&numbers=06XXXXXXXX

It looks like this solution is working but the api "refuses" it看起来这个解决方案正在运行,但 api “拒绝”它

Between curl_setopt($ch, CURLOPT_POSTFIELDS, POSTVARS); curl_setopt($ch, CURLOPT_POSTFIELDS, POSTVARS); which is refused and curl_setopt($ch, CURLOPT_POSTFIELDS, "text=MyMessage&numbers=06XXXXXXXX");这被拒绝并且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问题是它无法处理本地电话号码(06XXXXXXXX),但国际格式没有问题(+33),所以变量必须以“%2B33XXXXXXXX”开头,并且工作正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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