简体   繁体   English

如何使用内容类型为“ application / x-www-form-urlencoded”的PHP curl发送原始JSON?

[英]How can I send a raw JSON using PHP curl with the content type “application/x-www-form-urlencoded”?

How can I send a raw JSON using PHP curl with a content type of application/x-www-form-urlencoded ? 如何使用内容类型为application/x-www-form-urlencoded PHP curl发送原始JSON?

Let me explain: 让我解释:

I'm communicating with a webserver that accepts HTTP POST requests with a JSON object as the body of the request where normally we are used to seeing HTTP query parameters. 我正在与一个Web服务器进行通信,该Web服务器接受带有JSON对象的HTTP POST请求作为请求的主体,通常我们通常会在其中查看HTTP查询参数。

In my situation, I need to send a request with the following content-type 在我的情况下,我需要发送以下内容类型的请求

Content-Type: application/x-www-form-urlencoded 内容类型:application / x-www-form-urlencoded

The body must be raw JSON. 主体必须为原始JSON。

So, there are many possibilities. 因此,有很多可能性。 I tried the following: 我尝试了以下方法:

<?php
      $server_url = "http://server.com";
      $curl = curl_init($server_url);
      $data_array = array("a"=> "a_val", "b" => array("c"=>"c_val", "d"=>"d_val") );

 $options = array(
       CURLOPT_POST            => TRUE,
       CURLOPT_HTTPHEADER     => array('Content-Type: application/x-www-form-urlencoded'),
       CURLOPT_POSTFIELDS      => json_encode($data_array),
       CURLOPT_COOKIEJAR       => realpath('tmp/cookie.txt'),
       CURLOPT_COOKIEFILE      => realpath('tmp/cookie.txt')
        );

    curl_setopt_array($curl, $options);
    $return = curl_exec($curl);  
    var_dump($return);  
    curl_close($curl);
?>

I also tried to escape the json_encode() : 我也尝试转义json_encode()

...    
CURLOPT_POSTFIELDS      => "\"" . json_encode($data_array) .  "\"" ,
...

If the server was able to parse html parameters I could just do this: 如果服务器能够解析html参数,我可以这样做:

...    
CURLOPT_POSTFIELDS      => http_build_query($data_array)
...

However, that is not the case and I need a workaround. 但是,事实并非如此,我需要一种解决方法。

Please note that changing the content-type won't work. 请注意,更改内容类型无效。 I tried using text/plain , but the server would not accept it. 我尝试使用text/plain ,但是服务器无法接受。

Usually the application/x-www-form-urlencoded requires a key-value paired parameters for HTTP post. 通常, application/x-www-form-urlencoded需要用于HTTP发布的键值配对参数。 So it's very hard to suggest anything to you without seeing a sample POST data format. 因此,很难在不看到示例POST数据格式的情况下为您提供任何建议。 As per the document, you must place the URLencoded data with a variable. 根据文档,您必须将URL编码的数据放入一个变量。 For example your JSON should go like this. 例如,您的JSON应该像这样。

$post_data = "data=".urlencode(json_encode($data_array))

You can try sending the data without any key parameter, and it should not work 您可以尝试发送没有任何关键参数的数据,并且该数据不起作用

$post_data = urlencode(json_encode($data_array))

I'm not entirely sure I understand your question, so I'm going to answer two different versions. 我不太确定我是否理解您的问题,因此我将回答两个不同的版本。

Send JSON data, but with an (inaccurate) application/x-www-form-urlencoded Content Type 发送JSON数据,但是使用(不正确的) application/x-www-form-urlencoded内容类型

I don't know why you'd want to do this, but if you do, it should be fairly simple. 我不知道您为什么要这样做,但是如果这样做,它应该相当简单。

$data_array = array(
    'a' => 'a_val',
    'b' => array(
        'c' => 'c_val',
        'd' => 'd_val'
    )
);

$json = json_encode($data_array);

$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_USERAGENT, 'PHP/' . phpversion());
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $json);
curl_setopt($c, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
$result = curl_exec($c);
if (curl_errno($c)) {
    return trigger_error('CURL error [' . curl_errno($c) . '] ' . curl_error($c));
}
curl_close($c);

echo $result;

Bear in mind that you are, here, sending deliberately inaccurate data to the server. 请记住,这里是您故意向服务器发送不准确的数据。 You're sending JSON, but calling it urlencoded. 您正在发送JSON,但将其称为urlencoded。 You probably don't want to do this; 您可能不想这样做。 if, for some reason, you do need to do this, you'd probably be better off fixing whatever the real problem is, rather than using this hacky workaround. 如果由于某种原因确实需要这样做,那么最好解决所有实际问题,而不是使用此变通办法。

If you were using Guzzle rather than cURL, it might be a little trickier. 如果您使用的是Guzzle而不是cURL,则可能会有些棘手。 Guzzle has built-in support for both JSON and urlencoded, but if you want to mess about like this, you'd be better off not using that. Guzzle具有对JSON和urlencoded的内置支持,但是如果您想弄乱这种方式,最好不要使用它。 Generate your JSON data yourself (using $json = json_encode($data) ), and set the Content-Type in Guzzle by hand. 自己生成JSON数据(使用$json = json_encode($data) ),然后手动在Guzzle中设置Content-Type。

Send urlencoded JSON data 发送urlencoded JSON数据

This is an odd setup, but accurate. 这是一个奇怪的设置,但很准确。 At least you wouldn't be lying in your HTTP headers. 至少您不会躺在HTTP标头中。

Basically the same as above, but add this: 基本上与上述相同,但添加以下内容:

$json = json_encode($data_array);
$data = array('JSON' => $json);
$body = http_build_query($data);

And then set CURLOPT_POSTFIELDS to $body instead of to $json . 然后将CURLOPT_POSTFIELDS$body而不是$json

What you probably really should do: send JSON as JSON. 您可能真正应该做的是:将JSON作为JSON发送。

In most situations, you'd be better off sending JSON data (as in example one), and also setting the Content-Type to application/json . 在大多数情况下,最好发送JSON数据(如示例一所示),并将Content-Type设置为application/json This is a smaller data size than example two (the urlencoding step increases the size of the data), and it has accurate header data. 与示例二相比,这是一个较小的数据大小(urlencoding步骤会增加数据的大小),并且它具有准确的标头数据。

暂无
暂无

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

相关问题 Php CURL虽然我将标题设置为application / json,但请求仍然是application / x-www-form-urlencoded - Php CURL Though I set the header as application/json the request goes as application/x-www-form-urlencoded PHP - 假设应用程序/x-www-form-urlencoded 未指定内容类型 - PHP - Content-type not specified assuming application/x-www-form-urlencoded 如何通过在laravel中传递参数来获得内容类型application/x-www-form-urlencoded的响应 - How to get response of content type application/x-www-form-urlencoded by passing parameters in laravel 使用标题应用程序/ x-www-form-urlencoded进行卷曲发布 - curl posting with header application/x-www-form-urlencoded PHP curl将Content-Type更改为application / x-www-form-urlencoded。 不应该这样做 - PHP curl changes the Content-Type to application/x-www-form-urlencoded. Shouldn't do that file_get_contents():假设应用程序/x-www-form-urlencoded 未指定内容类型 - file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded 在PHP中使用cURL和x-www-form-urlencoded的POST返回访问被拒绝 - POST using cURL and x-www-form-urlencoded in PHP returning Access Denied ionic和php之间的application / x-www-form-urlencoded - application/x-www-form-urlencoded between ionic and php 使用Content-Type = application / x-www-form-urlencoded发送POST请求时,POST值为空 - POST value is empty when Sending POST request with Content-Type = application/x-www-form-urlencoded 如何使用内容类型从 webhook 获取数据:application/x-www-form-urlencoded;charset=UTF-8? - How to get data from webhook with content-type: application/x-www-form-urlencoded;charset=UTF-8?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM