cURL错误56:发送xml请求时从对等方接收数据时失败

时间:2018-11-13 10:10:02

标签: php guzzle guzzle6 guzzlehttp

我正在尝试将curl转换为使用guzzle,但出现错误cURL error 56: Failure when receiving data from the peer。 这是我到目前为止的内容。

curl_test.php正常工作

    $username = "admin";
    $password= "password";
    $request_xml = '<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE telco_xml_enq SYSTEM "telco_xml_enq.dtd">
      <person>
          <individual>
            <user_id>1234</user_id>
            <full_name>test</full_name>
          </individual>
        </body>
      </person>';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 4);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, "XML_STRING=" . $request_xml);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));
    $response     = curl_exec($ch);
    return $response;

guzzle_test.php无法正常工作

    $request_xml = '<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE telco_xml_enq SYSTEM "telco_xml_enq.dtd">
      <person>
          <individual>
            <user_id>1234</user_id>
            <full_name>test</full_name>
          </individual>
        </body>
      </person>';
    $client = new Client(["verify"=>false,
                "auth"=>["admin","password"
    ]
    ]);
    $options = [
        "headers"=>
            ["Content-Type"=>"text/xml"],
        "body"=>$requestXml
    ];
    $response = $client->request("POST",$url,$options]);
    $res = $response->getBody();

您能帮我吗?

非常感谢您的帮助。

谢谢

2 个答案:

答案 0 :(得分:0)

似乎您必须使用XML_STRING作为body中的帖子值。由于此接收器无法检索值,因此引发错误。

$options = [
    "headers"=>
        ["Content-Type"=>"text/xml"],
    "body" => "XML_STRING=".$requestXml
];

答案 1 :(得分:0)

您需要使用form_params而不是body来将表单发送到服务器。尝试下面的代码。

$request_xml = '<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE telco_xml_enq SYSTEM "telco_xml_enq.dtd">
  <person>
      <individual>
        <user_id>1234</user_id>
        <full_name>test</full_name>
      </individual>
    </body>
  </person>';
$client = new Client(["verify"=>false,
            "auth"=>["admin","password"
]
]);
$options = [
    "form_params"=>[
        'XML_STRING' => $requestXml,
    ],
];
$response = $client->request("POST",$url,$options]);
$res = $response->getBody();