PHP 个请求需要 body/form 个内容与报价一起发送

PHP requests need body/form content to be sent with quotations

我需要发送正文和报价才能使 http post 请求正常工作。正确的正文内容应如下所示:

{"pnr" : "123", "orgnr" : "456"}

问题:我如何使用下面的代码作为基础并得到上面的主体作为结果?

我正在使用此文档作为参考:https://github.com/rmccue/Requests/blob/master/docs/usage.md


尝试结果(摘要):

尝试 1:

"data": "{personal_number: 123, org_number: 456}"

尝试 2:

"data": "{personal_number: 123, org_number: 456}"

尝试 3:

"form": {"{personal_number: 123, org_number: 456}": ""

尝试 1 - 编码 json 字符串,在 [$data] 中发送正文。

require_once '../../../../packages/Requests/library/Requests.php';
Requests::register_autoloader();

$url      = 'http://httpbin.org/post';

$data = '{personal_number: 123, org_number: 456}';

$headers = array(
    'Accept-Encoding'  => 'gzip, deflate',
    'Accept'           => 'application/json, text/xml, application/xml, */*',
    'Content-Type'     => 'application/json; charset=UTF-8'
);

$response_1 = Requests::post($url, $headers, json_encode($data));

var_dump($response_1->body);

尝试 1 - 结果:

string(567) "{
  "args": {}, 
  "data": "\"{personal_number: 123, org_number: 456}\"", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "application/json, text/xml, application/xml, */*", 
    "Accept-Encoding": "gzip, deflate", 
    "Content-Length": "41", 
    "Content-Type": "application/json; charset=UTF-8", 
    "Host": "httpbin.org", 
    "Referer": "http://httpbin.org/post", 
    "User-Agent": "php-requests/1.7"
  }, 
  "json": "{personal_number: 123, org_number: 456}", 
  "origin": "82.117.105.239, 8"...

尝试 2 - 发送 json 字符串而不编码,在 [$data] 内发送正文。

require_once '../../../../packages/Requests/library/Requests.php';
Requests::register_autoloader();

$url      = 'http://httpbin.org/post';

$data = '{personal_number: 123, org_number: 456}';

$headers = array(
    'Accept-Encoding'  => 'gzip, deflate',
    'Accept'           => 'application/json, text/xml, application/xml, */*',
    'Content-Type'     => 'application/json; charset=UTF-8'
);

$response_2 = Requests::post($url, $headers, $data);

var_dump($response_2->body);

尝试 2 - 结果:

string(526) "{
  "args": {}, 
  "data": "{personal_number: 123, org_number: 456}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "application/json, text/xml, application/xml, */*", 
    "Accept-Encoding": "gzip, deflate", 
    "Content-Length": "39", 
    "Content-Type": "application/json; charset=UTF-8", 
    "Host": "httpbin.org", 
    "Referer": "http://httpbin.org/post", 
    "User-Agent": "php-requests/1.7"
  }, 
  "json": null, 
  "origin": "82.117.105.239, 82.117.105.239", 
  "url": "https://httpbi"...

尝试 3 - 发送 json 字符串而不编码,在 [form] 内发送正文。

require_once '../../../../packages/Requests/library/Requests.php';
Requests::register_autoloader();

$url      = 'http://httpbin.org/post';

$data = '{personal_number: 123, org_number: 456}';

$headers = array(
    'Accept-Encoding'  => 'gzip, deflate',
    'Accept'           => 'application/json, text/xml, application/xml, */*',
    'Content-Type'     => 'application/json; charset=UTF-8'
);

$response_2 = Requests::post($url, array(), $data);

var_dump($response_3->body);

尝试 3 - 结果:

string(497) "{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "{personal_number: 123, org_number: 456}": ""
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "deflate, gzip", 
    "Content-Length": "39", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "Referer": "http://httpbin.org/post", 
    "User-Agent": "php-requests/1.7"
  }, 
  "json": null, 
  "origin": "82.117.105.239, 82.117.105.239", 
  "url": "https://httpbin.org/post"
}

创建数组 然后设置键及其值 最后,它应该包裹在 json_encode.

$data = Array();
$data["personal_number"] = "123";
$data["org_number"] = "456";

$response_2 = Requests::post($url, $headers, json_encode($data));