简体   繁体   中英

Can I post both JSON body and POST values at the same time using PHP/CURL?

I have a web service that accepts GET, POST values as parameters. So far I used to call the data using a simple CURL script, as below:

http://localhost/service/API.php?key=some_password&request=some_request&p1=v1&p2=v2

This is successfully posted using "CURLOPT_POSTFIELDS":

    $base_args = array(
        'key' => 'some_password',
        'request' => 'some_request',
    );

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");   
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);

The problem is, I need to also POST a JSON body to this service. I found out that this can be done by using "CURLOPT_POSTFIELDS", but also found out that I am not able to use both POST and JSON.

$data_string ="{'test':'this is a json payload'}";

$ch = curl_init('http://localhost/service/API.php');

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);
echo ($result); // string returned by the service.

I can either set the POST values, OR set the JSON body, but not both. Is there a way I can push the two different types of values?

Try sending the json text via a variable:

$data = array('data' => "{'test':'this is a json payload'}", 'key' => 'some_password', 'request' => 'some_request');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

Then in your API you do:

var_dump(json_decode($_POST['data']));

You can still use the other key/request variables.

I could not find any way of sending both POST and JSON body at the same time. Therefore the workaround was to add the POST arguments as part of the JSON body.

$base_args = array(
    'key' => '8f752Dd5sRF4p',
    'request' => 'RideDetails',
    'data' => '{\'test\',\'test\'}',
);

$ch = curl_init('http://localhost/service/API.php');                                                                      

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");   
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($base_args));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER,    array(                                                                          
'Content-Type: application/json',                                                                         
'Content-Length: ' . strlen(http_build_query($base_args)))  

);

$result = curl_exec($ch);
echo ($result);

The full JSON can be returned by the API as below:

var_dump(json_decode(file_get_contents('php://input')));

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