繁体   English   中英

通过 PHP ZF6E57C9DE709E45FEB40D955351F8Z 发送 XML 后提取 XML 响应时出现问题

[英]Problem extracting XML response after sending XML via PHP curl

我的以下代码有一些问题:

<?php
$url = "192.168.0.1:10040";

$xml = '<root><list><MatrixConnectionList><DviConsole type="name">Monitor X</DviConsole></MatrixConnectionList></list></root>';

$headers = array(
    "Content-type: text/xml",
    "Content-length: " . strlen($xml),
    "Connection: close",
);


//Execute the curl init
$ch = curl_init();

//Add URL to Curl
curl_setopt($ch, CURLOPT_URL, $url);

//Return response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//set timeout
curl_setopt($ch, CURLOPT_TIMEOUT, 5);

//Add post option to curl
curl_setopt($ch, CURLOPT_POST, true);

//Add curel post field
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);

//Add headers
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

//Add verbose for error logging
curl_setopt($ch, CURLOPT_VERBOSE, true);

//Execute Curl
$data = curl_exec($ch);

//Print response
error_log(print_r($xml, TRUE));
error_log(print_r($data, TRUE));

//Log errors and close curl
if(curl_errno($ch))
    print curl_error($ch);
else
    curl_close($ch);
?>

响应应该是这样的:

<?xml version="1.0" encoding="utf-8"?><root><result type="list"><MatrixConnectionList><item><cpuId>0x0000</cpuId><cpuCl>DviCpu</cpuCl><cpuName>MISC</cpuName><cpuPoweredOn>true</cpuPoweredOn><signalType>viewonly</signalType><consoleId>0x000000</consoleId><consoleCl>DviConsole</consoleCl><consoleName>Monitor X</consoleName><connectionOwnerId>0x00000000</connectionOwnerId><connectionOwnerCl>DviMatrix</connectionOwnerCl><connectionOwnerPort>2</connectionOwnerPort><connectionOwnerName>Matrix X</connectionOwnerName><consoleConfigEnable>1</consoleConfigEnable><consolePoweredOn>true</consolePoweredOn><userName>0000000</userName><transmission>2</transmission></item></MatrixConnectionList></result></root>

除了当我检查我的 Apache 错误日志时,我看到以下内容:

* upload completely sent off: 118 out of 118 bytes
* Operation timed out after 5001 milliseconds with 1456 out of -1 bytes received
[Thu Apr 30 10:48:05.967278 2020] [php7:notice] [pid 20251] [client 192.168.0.2:6000] , referer: http://192.168.0.1/pages/test.php

我在这里错过了什么吗? 为什么 curl_exec 不将返回的字符串分配给我的变量。

谢谢!

您要访问的服务器没有发送Content-Length header (根本不发送,或者发送格式错误的,我不确定,您可以查看详细日志以找出答案),这使得 curl 只是继续阅读,直到远程服务器关闭套接字,并且服务器使用超过 5 秒来关闭套接字,并且 5 秒似乎是您的默认 CURLOPT_TIMEOUT,所以您收到超时错误.. 当您收到 CURLOPT_RETURNTRANSFER 错误时, curl_exec() 不返回响应字符串,而是返回 bool(false) 以指示发生错误。

您可以尝试增加 CURLOPT_TIMEOUT 以查看服务器是否最终会关闭套接字,

// timeout at 10 seconds instead of 5, server reponds slowly sometimes..
curl_setopt($ch,CURLOPT_TIMEOUT,10);

或者你可以删除

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

并添加

$response="";
curl_setopt($ch,CURLOPT_WRITEFUNCTION,function($ch,string $data)use(&$response):int{
    $response.=$data;
    return strlen($data);
});
$error_if_false=curl_exec($ch);
if(false===$error_if_false){
     // error, but your response is now in $response regardless.
}

暂无
暂无

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

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