简体   繁体   English

我可以使用PHP / CURL同时发布JSON正文和POST值吗?

[英]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. 我有一个Web服务,它接受GET,POST值作为参数。 So far I used to call the data using a simple CURL script, as below: 到目前为止,我以前使用简单的CURL脚本来调用数据,如下所示:

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

This is successfully posted using "CURLOPT_POSTFIELDS": 这是使用“ 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. 问题是,我还需要向该服务发布JSON正文。 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. 我发现可以通过使用“ CURLOPT_POSTFIELDS”来完成此操作,但同时发现我无法同时使用POST和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. 我可以设置POST值,也可以设置JSON正文,但不能同时设置两者。 Is there a way I can push the two different types of values? 有没有办法可以推两种不同类型的值?

Try sending the json text via a variable: 尝试通过变量发送json文本:

$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: 然后在您的API中执行以下操作:

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. 我找不到同时发送POST和JSON正文的任何​​方式。 Therefore the workaround was to add the POST arguments as part of the JSON body. 因此,解决方法是将POST参数添加为JSON主体的一部分。

$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: API可以返回完整的JSON,如下所示:

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

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

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