使用PHP scopClient代理问题处理
因为网络问题导致访问接口速度太慢,需添加代理,以下使用的是socks5,访问超时导致解析错误,返回:looks like we got no XML document 错误
public function getSoap()
{
if (!$this->soap) {
// $this->soap = new SoapClient(static::WSDL);
$this->soap = new ProxySoapClient(static::WSDL);//网络问题配置代理
}
return $this->soap;
}
重新请求方法,通过curl配置代理信息
use SoapClient;
class ProxySoapClient extends SoapClient
{
protected function callCurl($url, $data, $action)
{
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $url);
// If you need to handle headers like cookies, session id, etc. you will have
// to set them here manually
$headers = array("Content-Type: text/xml", 'SOAPAction: "' . $action . '"');
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
curl_setopt($handle, CURLOPT_FRESH_CONNECT, true);
curl_setopt($handle, CURLOPT_HEADER, true);
//使用sock代理
curl_setopt($handle, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:20.0) Gecko/20100101 Firefox/20.0');//设置useragent
curl_setopt($handle, CURLOPT_HTTPPROXYTUNNEL, true);
curl_setopt($handle, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);//使用SOCKS5协议
curl_setopt($handle, CURLOPT_PROXY, "localhost");// 代理服务器IP
curl_setopt($handle, CURLOPT_PROXYPORT, "1234");//代理服务器端口
$response = curl_exec($handle);
curl_close($handle);
list($headers, $content) = explode("\r\n\r\n", $response, 2);
/*返回100后这种会导致解析错误
HTTP/1.1 100 Continue
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/xml; charset=utf-8
Date: Thu, 26 Mar 2020 03:37:28 GMT
Content-Length: 2522
*/
if($response){
$result = explode("\r\n\r\n", $response);
$content = array_pop($result);
}
return $content;
}
public function __doRequest($request, $location, $action, $version, $one_way = 0)
{
return $this->callCurl($location, $request, $action);
}
}
返回HTTP/1.1 100 Continue时解析会出错,会提示:looks like we got no XML document, 需要根据实际返回情况进行处理,保证正确解析
参考文档:http://www.livingwithphp.com/php-soap-client-socks-proxy/