如何用PHP通过SOAP协议调用API实现复杂业务流程?

2026-04-02 18:071阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计565个文字,预计阅读时间需要3分钟。

如何用PHP通过SOAP协议调用API实现复杂业务流程?

PHP使用SOAP调用API操作示例:将图片转换为base64编码格式。

php/** * 将图片转换为base64编码格式 * * @param $image_file 图片文件路径 * @return string 返回base64编码后的字符串 */function base64EncodeImage($image_file) { $base64_image=''; $image_info=getimagesize($image_file); $base64_image='data:' . $image_info['mime'] . ';base64,' . base64_encode(file_get_contents($image_file)); return $base64_image;}

本文实例讲述了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 "<br/>"; echo($obj->ExtraCode); echo "<br/>"; echo($obj->Code); echo "<br/>"; 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连接第三方的webservice,是urlToSoapWs", array( 'stream_context' => $streamContext ));

禁止引用外部xml实体

如何用PHP通过SOAP协议调用API实现复杂业务流程?

libxml_disable_entity_loader(true);

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: “outofmemory.cn/code-snippet/tagged/canvas“

报这个错误之后,整个服务器就不响应了,但是nginx后面的webpy程序没有任何错误,后端的数据库也很正常,从网上查了很多资料,都是说要修改proxy_read_timeout,proxy_send_timeoutproxy_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相关内容感兴趣的读者可查看本站专题:《php文件操作总结》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

本文共计565个文字,预计阅读时间需要3分钟。

如何用PHP通过SOAP协议调用API实现复杂业务流程?

PHP使用SOAP调用API操作示例:将图片转换为base64编码格式。

php/** * 将图片转换为base64编码格式 * * @param $image_file 图片文件路径 * @return string 返回base64编码后的字符串 */function base64EncodeImage($image_file) { $base64_image=''; $image_info=getimagesize($image_file); $base64_image='data:' . $image_info['mime'] . ';base64,' . base64_encode(file_get_contents($image_file)); return $base64_image;}

本文实例讲述了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 "<br/>"; echo($obj->ExtraCode); echo "<br/>"; echo($obj->Code); echo "<br/>"; 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连接第三方的webservice,是urlToSoapWs", array( 'stream_context' => $streamContext ));

禁止引用外部xml实体

如何用PHP通过SOAP协议调用API实现复杂业务流程?

libxml_disable_entity_loader(true);

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: “outofmemory.cn/code-snippet/tagged/canvas“

报这个错误之后,整个服务器就不响应了,但是nginx后面的webpy程序没有任何错误,后端的数据库也很正常,从网上查了很多资料,都是说要修改proxy_read_timeout,proxy_send_timeoutproxy_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相关内容感兴趣的读者可查看本站专题:《php文件操作总结》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。