简体   繁体   中英

Curl JSON request in PHP

I am trying to send the following cURL request in PHP:

$ 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:

    <?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?

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:

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:

$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.

I think it is how you are building the json string...

You start by defining $data as an array of strings, and then json_encode it. But it is already in json format anyway (from a quick eyeball check).

The json_encode (and _decode) are meant to work with an associative array for your data.

Or just send the data string you are building, just check that it is in correct json format first.

<?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.

And, of course, check wich data are you sending in post. It seems incorrect to json_encode an array with an element "string"..

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:

 $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);  

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