使用PHP scopClient代理问题处理 looks like we got no XML document

使用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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值