PHP calling Java webservice those of the pit

Why call the webservice interface to it?

  • The company took a hardware project, the hardware interface uses Java project webservice interface technology written.

What is the webservice?

  • Web Services are application components
  • Web Services communicate using open protocols
  • Web Services is an independent (self-contained) and self-describing
  • Web Services can be found by using UDDI
  • Web Services can be used by other applications
  • XML Web Services is the basis

php webservice calls in two ways

  • php extension class soap
  • The php CURL

What is soap

soap request (Simple Object Access Protocol, Simple Object Access Protocol) is a special version of the HTTP POST, following a special message format xml Content-type to: text / xml xml can be of any data.

SOAP: Simple Object Access Protocol, Simple Object Access Protocol (SOAP) is a lightweight, simple, XML-based protocol that is designed to be cured and exchanging structured information in a WEB. SOAP can be used and many of the existing Internet protocols and formats combination, including the Hypertext Transfer Protocol (HTTP), Simple Mail Transfer Protocol (SMTP), Multipurpose Internet Mail Extensions (MIME). It also supports call (RPC), a large number of applications from messaging systems to remote procedure.

Xiao Bian here do not use soap php, do not do too much for soap introduced.

Using curl to call the webservice interface to Java

Without further ado, Syria directly on the code

Create a curl

function posturl($url,$data){
    $headerArray =array("Content-Type: application/soap+xml; charset=utf-8", "Content-length: ".strlen($data));
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}

Webservice interface method using the acquired format SoapUI

Download online tool soapUI

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Copy the above parameters format written out php written inside.

Xiao Bian here highly recommended PHPstorm tool because the data is copied into it, PHPstorm tools will help us parse the data.
$url = "http://.cn:17236/vims/services/MobPhoneService?wsdl";
$code = 1000001;
$data = "
<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservice.dhsoft.com\">
   <soapenv:Header/>
   <soapenv:Body>
      <web:openDoor>
         <!--Optional:-->
         <arg0>
         		{\"deviceCode\":$code}
         </arg0>
      </web:openDoor>
   </soapenv:Body>
</soapenv:Envelope>";

调用一下看看数据,由于用的是http的请求方式,所以webservice返回的数据里面有好多没用的东西,但是直接在浏览器打印的时候看到的只有json串,因为浏览器把xml那部分解析了。
var_dump(posturl($url,$data));

1,返回的数据格式:
	string(307) "{"status":"1","resultMessage":"信息发送成功"}"

2,其实是浏览器把xml的部分解析了,下面我们查看源代码看一下真实的数据吧;

	string(307) "<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:openDoorResponse 
	xmlns:ns2="http://webservice.dhsoft.com"><openDoor>{&quot;status&quot;:&quot;1&quot;,&quot;resultMessage&quot;:&quot;信息发送成功&quot;}
	</openDoor></ns2:openDoorResponse></soap:Body></soap:Envelope>"

3,我们看到这不是json数据,怎么办呢,小编在网上费了九牛二虎之力找到了字符串截取的办法。
	function get_between($input, $start, $end) {
	    $str = substr($input, strlen($start)+strpos($input, $start),(strlen($input) - strpos($input, $end))*(-1));
	    return $str;
	}
	$data = '{'.get_between(posturl($url,$data),'{','}').'}';
	$data_str = str_replace('&quot;','"',$data);
	var_dump($data_str);
	
4,此时我们在使用查看源码的方式,数据格式就是完整的json数据。
	string(51) "{"status":"1","resultMessage":"信息发送成功"}"

5,这个时候用json_decode把json转换成数据是轻而易举了。
	var_dump(json_decode($data_str));
	object(stdClass)#1 (2) {
	  ["status"]=>
	  string(1) "1"
	  ["resultMessage"]=>
	  string(18) "信息发送成功"
	}

to sum up

Often when we call other api, the data format error, the browser can be deceiving, we have to use to view the source code the way to resolve what the real data, Xiao Bian is experiencing this pit. Hang me for several days, obviously browser print is complete json data, but using json_decode is to parse out the array. Later, a look at the source code. The original hidden inside so much data.

I wish little friends encountered such a pit in time to climb out of.

Guess you like

Origin blog.csdn.net/zxh7770/article/details/88568229