PHP curl请求发送时没有头


PHP curl request being sent with no headers

我非常感谢您的帮助。在这个问题上我没有任何进展。

从这里开始是工作代码

$client = new SoapClient("/var/www/$schema/$space.wsdl", array('trace' => 1));
try {
    $result = $client->$service($request);
}
catch (SoapFault $soapFault) {
    //print_r($soapFault);
    echo "ERROR: ". $client->__getLastRequestHeaders ();
    exit();
}
echo "SUCCESS: ". $client->__getLastRequestHeaders ();
exit();

我从soap调用和上面的代码输出中得到了适当的数据。。。

SUCCESS: POST /server/ws/r/customer HTTP/1.1
Host: soap.server.com
Connection: Keep-Alive
User-Agent: PHP-SOAP/5.3.3
Content-Type: text/xml; charset=utf-8
SOAPAction: ""
Content-Length: 287

现在,当我尝试扩展SoapClient(添加重试和超时)时,

include_once("/var/www/object/soapTest.php");
$client = new SoapTest("/var/www/$schema/$space.wsdl", array('trace' => 1,'timeout' => $timeout_length,'connecttimeout' => $timeout_length));
try {
    $result = $client->$service($request);
}
catch (SoapFault $soapFault) {
    //print_r($soapFault);
    echo "ERROR: ". $client->__getLastRequestHeaders ();
    exit();
}
echo "SUCCESS: ". $client->__getLastRequestHeaders ();
exit();

我得到的只是错误:没有其他信息。所以我尝试了两个第三方库来扩展SoapClient。。。两者的结果相同:

https://github.com/ideaconnect/idct-soap-client/blob/master/src/client.php

https://gist.github.com/RobThree/2490351

我当前的代码如下:

public function __doRequest($request, $location, $action, $version, $one_way = FALSE)
{
    if (($this->timeout===0) && ($this->connecttimeout===0))
    {
        // Call via parent because we require no timeout
        $response = parent::__doRequest($request, $location, $action, $version, $one_way);
    }
    else
    {
        // Call via Curl and use the timeout
        $curl = curl_init($location);
        if ($curl === false)
            throw new Exception('Curl initialisation failed');
        $header = array("Content-Type:text/xml;charset=utf-8","User-Agent:PHP-SOAP/5.3.3","SOAPAction:'"".$action."'"");
        $options = array(
            CURLOPT_VERBOSE => false,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => $request,
            CURLOPT_HEADER => true,
            CURLOPT_NOSIGNAL => false
            CURLOPT_HTTPHEADER => $header
            //CURLOPT_SSL_VERIFYPEER => $this->sslverifypeer
        );
        //print_r($options);
        //exit();
        if ($this->timeout>0) {
            if (defined('CURLOPT_TIMEOUT_MS')) {    //Timeout in MS supported?
                $options[CURLOPT_TIMEOUT_MS] = $this->timeout;
            } else  { //Round(up) to second precision
                $options[CURLOPT_TIMEOUT] = ceil($this->timeout/1000);
            }
        }
        if ($this->connecttimeout>0) {
            if (defined('CURLOPT_CONNECTTIMEOUT_MS')) { //ConnectTimeout in MS supported?
                $options[CURLOPT_CONNECTTIMEOUT_MS] = $this->connecttimeout;
            } else { //Round(up) to second precision
                $options[CURLOPT_CONNECTTIMEOUT] = ceil($this->connecttimeout/1000);
            }
        }
        if (curl_setopt_array($curl, $options) === false)
            throw new Exception('Failed setting CURL options');
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        $response = curl_exec($curl);
        if (curl_errno($curl)){
            throw new Exception(curl_error($curl));
        }
        curl_close($curl);
    }
    // Return?
    if (!$one_way)
        return ($response);
}

我还试着从选项数组中提取头声明,并使用进行操作

curl_setopt($curl, CURLOPT_HTTPHEADER, $header);

curl_setopt_array()之后。。。。没有运气。

有什么想法吗?我不知道发生了什么。

使用curl时,不使用parent::__doRequest,因此soapClient无法跟踪您的请求。

您的代码给人的印象是,您使用curl是因为您认为SoapClient无法设置请求超时。这是不对的。

http://php.net/manual/en/soapclient.soapclient.php

connection_timeout选项为连接到SOAP服务。此选项不定义超时对于响应缓慢的服务。限制等待呼叫的时间以完成defaultsocket_timeout设置。