PHP SOAP调用基础与常见错误排查
本文分享一个PHP利用SOAP协议调用API接口的完整实例,供开发者参考学习。
/*图片转换为 base64格式编码*/
function base64EncodeImage($image_file)
{
$base64_image = '';
$image_info = getimagesize($image_file);
$image_data = fread(fopen($image_file, 'r'), filesize($image_file));
//$base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
$base64_image = chunk_split(base64_encode($image_data));
return $base64_image;
}
$strPhotoFront_base64 = base64EncodeImage("static/img/a.png");
$strPhotoRear_base64 = base64EncodeImage("static/img/b.png");
$paras["strPhotoFront"] = $strPhotoFront_base64;
$paras["strPhotoRear"] = $strPhotoRear_base64;
$paras["strSecretKey"] = "";
$wsdl = "";
$client = new SoapClient($wsdl);
$soapParas = array($paras);
$outString = $client->__soapCall("UploadPhotoId", $soapParas);
$obj = simplexml_load_string($outString->UploadPhotoIdResult->any);
echo($obj->ExtraInfo);
echo "
";
echo($obj->ExtraCode);
echo "
";
echo($obj->Code);
echo "
";
echo($obj->Message);
如果在运行过程中遇到“Fatal error: Class 'SoapClient' not found”的错误提示,请参考《PHP Class SoapClient not found解决方法》一文进行排查。
此外,若出现“SOAP-ERROR: Parsing WSDL:Couldn't load from “xxxxxxx” ”的解析错误,可参考相应的解决方案。
当使用PHP的SoapClient连接第三方的HTTPS Webservice接口时,可能会报出“SOAP-ERROR: Parsing WSDL:Couldn't load from “xxxxxxx” ”的错误。
首先应检查服务器的PHP环境中是否已正确安装并启用soap扩展。
其次确认服务器是否安装了openssl扩展。
确保服务器操作系统层面已正确安装openssl组件。
排除第三方服务对当前服务器IP地址的限制可能性。
最后,怀疑是因为HTTPS请求需要进行SSL验证,而本机环境缺乏必要的pem证书文件导致连接失败。
可以通过以下配置设置来忽略SSL验证步骤。
其中verify_peer参数用于指定是否验证SSL证书,默认值为true。
将该参数设置为false以跳过验证。
另外,还需要允许引用外部XML实体以避免解析错误。
执行libxml_disable_entity_loader(false);语句来启用外部实体引用。
libxml_disable_entity_loader(false);
$opts = array(
'ssl' => array(
'verify_peer' => false
),
'https' => array(
'curl_verify_ssl_peer' => false,
'curl_verify_ssl_host' => false
)
);
$streamContext = stream_context_create($opts);
$client = new SoapClient("https://urlToSoapWs",
array(
'stream_context' => $streamContext
));
若需禁止引用外部XML实体,请使用其他配置方式。
libxml_disable_entity_loader(true);
Nginx Upstream Timed Out 错误解决
针对Nginx报错“upstream timed out (110: Connection timed out)”的问题,以下是解决方案。
Nginx每隔几个小时就会报下面的错误:
2013/05/18 21:21:36 [error] 11618#0: *324911 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 42.62.37.56, server: localhost, request: “GET /code-snippet/2747/HTML5-Canvas-usage HTTP/1.0”, upstream: “fastcgi://127.0.0.1:9002”, host: “outofmemory.cn”, referrer: “http://outofmemory.cn/code-snippet/tagged/canvas“
报这个错误之后,整个服务器就不响应了,但是nginx后面的webpy程序没有任何错误,后端的数据库也很正常,从网上查了很多资料,都是说要修改proxy_read_timeout,proxy_send_timeout和proxy_buffer几个相关设置的值。
如下配置,要放在server配置节之内
large_client_header_buffers 4 16k; client_max_body_size 30m; client_body_buffer_size 128k; proxy_connect_timeout 300; proxy_read_timeout 300; proxy_send_timeout 300; proxy_buffer_size 64k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; fastcgi_connect_timeout 300; fastcgi_read_timeout 300; fastcgi_send_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 32k; fastcgi_busy_buffers_size 64k; fastcgi_temp_file_write_size 64k;
你可以看到上面是proxy_和fastcgi_两种配置,就是说如果你的nginx后面是proxy,就设置proxy相关的配置,如果是fastcgi就设置fastcgi相关的配置。
希望本文所述对大家PHP程序设计有所帮助。