[英]Curl JSON request in PHP
I am trying to send the following cURL request in PHP: 我正在尝试在PHP中发送以下cURL请求:
$ curl -H 'Content-Type: application/json' -d '{"username":"a", "password":"b","msisdn":"447000000001","webhook":"http://example.com/"}' https://ms.4url.eu/lookup
Which should return: 应该返回:
198 bytes text/html; charset=UTF-8
{
"id": "ea26d0b2-b839-46b9-9138-50cc791bab47",
"msisdn": "447825001771",
"status": "Success",
"networkCode": "23471",
"countryName": "UK",
"countryCode": "GBR",
"network": "O2",
"networkType": "GSM",
"ported": "No"
}
I have tried to implement the code to send a request using cURL like so: 我试图实现使用cURL发送请求的代码,如下所示:
<?php
$data = array('{"username":"a", "password":"b", "msisdn":"447123121234", "webhook":"http://1f89e4a8.ngrok.io"}');
$data_string = json_encode($data);
$ch = curl_init('http://ms.4url.eu/lookup');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
?>
Using this method nothing seems to be happening I cannot see the JSON data being sent to ms.4url.eu/lookup, and when I try to echo $result I get no data echoed out? 使用这种方法似乎什么也没发生,我看不到发送到ms.4url.eu/lookup的JSON数据,当我尝试回显$ result时,没有回显任何数据吗?
Any help is much appreciated. 任何帮助深表感谢。
A Successful curl request is showing: 成功的卷曲请求显示:
POST /testDir/getPost.php HTTP/1.1
host: 1f89e4a8.ngrok.io
accept: application/json
content-type: application/json
content-length: 198
Connection: close
X-Forwarded-For: 5.44.233.221
{"id":"ea26d0b2-b839-46b9-9138-50cc791bab47","msisdn":"447123121234","status":"Success","networkCode":"23471","countryName":"UK","countryCode":"GBR","network":"O2","networkType":"GSM","ported":"No"}
The post request from my PHP code is showing: 我的PHP代码中的发布请求显示:
GET /testDir/curlPost.php HTTP/1.1
Accept: text/html, application/xhtml+xml, image/jxr, */*
Accept-Language: en-GB
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393
Accept-Encoding: gzip, deflate
Host: 1f89e4a8.ngrok.io
X-Forwarded-For: 92.11.143.199
Overall I would like to send the curl request from sendRequest.php and receive the Post for the webhook to getPost.php possibly using: 总的来说,我想从sendRequest.php发送curl请求,并可能使用以下方式将Webhook的Post接收到getPost.php:
$entityBody = file_get_contents('php://input');
if ($entityBody != null){
echo $entityBody;
}
at the Minute I am using the getPost.php to send the HTTP 200 OK so ms.4url.eu stops sending requests 302 Found. 在分钟时,我正在使用getPost.php发送HTTP 200 OK,因此ms.4url.eu停止发送请求302 Found。
I think it is how you are building the json string... 我认为这就是您构建json字符串的方式...
You start by defining $data as an array of strings, and then json_encode it. 首先将$ data定义为字符串数组,然后对其进行json_encode编码。 But it is already in json format anyway (from a quick eyeball check).
但无论如何,它已经是json格式(通过快速的眼球检查)。
The json_encode (and _decode) are meant to work with an associative array for your data. json_encode(和_decode)用于与数据的关联数组一起使用。
Or just send the data string you are building, just check that it is in correct json format first. 或者只是发送您要构建的数据字符串,只需先检查它是否采用正确的json格式即可。
<?php
// build an associative array
$data["username"]="a";
$data["password"]="b";
$data["msisdn"]="447123121234";
$data["webhook"]="http://1f89e4a8.ngrok.io";
// turn it into json formatted string
$json_data=json_encode($data);
print_r($data);
print($json_data);
?> ?>
This gives you something like 这给你像
Array
(
[username] => a
[password] => b
[msisdn] => 447123121234
[webhook] => http://1f89e4a8.ngrok.io
)
{"username":"a","password":"b","msisdn":"447123121234","webhook":"http:\/\/1f89e4a8.ngrok.io"}
Try to get like raw post data like below 尝试获得像下面这样的原始帖子数据
<?php
$fp = fopen('php://input', 'r');
$rawData = stream_get_contents($fp);
echo "<pre>";
print_r($rawData);
echo "</pre>";
If you send only the json string via curl you have to use, on the destination page, php://input
to retrieve the data, because there is not key => value and the variables $_POST and $_REQUEST don't intersect the request. 如果您仅通过curl发送json字符串,则必须在目标页面上使用
php://input
来检索数据,因为没有key => value且变量$ _POST和$ _REQUEST不与请求。
And, of course, check wich data are you sending in post. 而且,当然,检查这些数据是否在邮寄中发送。 It seems incorrect to json_encode an array with an element "string"..
用元素“字符串”对数组进行json_encode似乎是不正确的。
If you want to retrieve the request from the $_POST or $_REQUEST variable it's better if you put your json data into a key using the http_build_query
function like following: 如果要从$ _POST或$ _REQUEST变量检索请求,最好使用
http_build_query
函数将json数据放入键中,如下所示:
$data_string = json_encode($data);
$ch = curl_init('http://ms.4url.eu/lookup');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('data' => $data_string)));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.