PHP Using SOAP to Call API Operation Sample
- 2021-11-10 09:10:44
- OfStack
This article example shows that PHP uses SOAP to call API operation. Share it for your reference, as follows:
/* Picture is converted to base64 Format encoding */
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);
Note: For the prompt: Fatal error: Class 'SoapClient' not found, please refer to "PHP Class SoapClient not found Solution"
Attachment: SOAP-ERROR: Parsing WSDL: Couldn 't load from "xxxxxxx" Solution
Connect webservice of the third party with soapclient of php, which is https, and report error of connection SOAP-ERROR: Parsing WSDL: Couldn 'ES40load from "xxxxxxx"
First, check whether the soap extension of php is installed
openssl Extension
The server itself installs openssl
Exclude the third party's IP restrictions on this server
Finally, it is suspected that https requires ssl authentication, and there is no pem file on this machine
ssl authentication can be ignored by setting the following
verify_peer: Specifies whether to verify ssl, default to true
Set verify_peer to false
In addition, it is allowed to reference external xml entities
Plus
libxml_disable_entity_loader(false);
Statement
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
));
Reference to external xml entities is prohibited
libxml_disable_entity_loader(true);
nginx Error Report upstream timed out (110: Connection timed out) Solution
nginx reports the following error every few hours:
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-ES112HTTP/1. 0",
upstream: "fastcgi://127.0. 0.1: 9002", host: "outofmemory. cn", referrer: "http://outofmemory.cn/code-snippet/tagged/canvas"
After this error, the entire server does not respond, but nginx behind the webpy program without any errors, the back-end database is also very normal, from the network to check a lot of information, are said to modify
proxy_read_timeout
,
proxy_send_timeout
And
proxy_buffer
Values for several related settings.
The following configuration should be placed in the server configuration section
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;
You can see that the above configurations are proxy_ and fastcgi_, that is, if your nginx is followed by proxy, set the configuration related to proxy, and if it is fastcgi, set the configuration related to fastcgi.
For more readers interested in PHP related contents, please check the special topics of this site: "php File Operation Summary", "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithm Summary", "PHP Array (Array) Operation Skills Complete Book", "php String (string) Usage Summary" and "php Common Database Operation Skills Summary"
I hope this paper is helpful to everyone's PHP programming.