Home >Backend Development >PHP Tutorial >PHP calls web services two methods soap and curl

PHP calls web services two methods soap and curl

WBOY
WBOYOriginal
2016-07-25 08:42:201297browse

Take http://www.webxml.com.cn/zh_cn/index.aspx

1. Use soap call

  1. //The server supports soap extension:
  2. /*Example 1:
  3. $client = new SoapClient("http://fy.webxml.com.cn/webservices /EnglishChinese.asmx?wsdl");
  4. $parameters = array("wordKey"=>"test");
  5. //Chinese and English bidirectional translation return data: array
  6. $result = $client->TranslatorString($parameters );
  7. echo "
    ";</li>
    <li> print_r($result->TranslatorStringResult)."<br />";</li>
    <li> echo "
    ";
  8. //Chinese and English bidirectional translation return array Sentence examples:
  9. $result1 = $client->Translator($parameters);
  10. echo "
    ";</li>
    <li> print_r($result1->TranslatorResult)."<br />";</li>
    <li> echo "
    ";
  11. //Get candidate words:
  12. $result2 = $client->SuggestWord($parameters);
  13. echo "
    ";</li>
    <li> print_r($result2->SuggestWordResult). "<br />";</li>
    <li> echo "
    ";
  14. //Get the read MP3 byte stream, return data: byte array Byte[]
  15. $result3 = $client->GetMp3($ parameters);
  16. echo "
    ";</li>
    <li> print_r($result3)."<br />";</li>
    <li> echo "
    ";
  17. */
  18. /*Example2:
  19. $client = new SoapClient("http://webservice.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl");
  20. $param = array('theIpAddress'=>'202.96.134.33');
  21. $ result = $client->getCountryCityByIp($param);
  22. echo "
    ";</li>
    <li> print_r($result->getCountryCityByIpResult);</li>
    <li> echo "
    ";
  23. $result1 = $client ->getGeoIPContext($param);
  24. echo "
    ";</li>
    <li> print_r($result1);</li>
    <li> echo "
    ";
  25. $result2 = $client->getVersionTime(
  26. );
  27. echo "
    ";</li>
    <li> print_r($result2);</li>
    <li> echo "
    ";
  28. */
  29. //Example3:
  30. $client = new SoapClient("http://webservice. webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl");
  31. //Get the province, region and mobile phone card type information of the domestic mobile phone number
  32. $parm=array('mobileCode'=>'1367007',' userID'=>'');
  33. $result=$client->getMobileCodeInfo($parm);
  34. echo ($result->getMobileCodeInfoResult)."
    ";
  35. //Get the domestic mobile phone number Geodatabase information
  36. $result1 = $client->getDatabaseInfo($parm);
  37. print_r($result1)."
    ";
  38. // Get the list of SOAP types (Returns list of SOAP types)
  39. echo '
    ';</li>
    <li> print_r($client->__getTypes ()) ;</li>
    <li> echo '
    ';
  40. // Get the functions provided by webservice
  41. echo '
    ';</li>
    <li> print_r( $client->__getFunctions ()) ;</li>
    <li> echo '
    ';
  42. //If the server does not support soap extension, you can introduce an online open source class library
  43. ?>
Copy the code


2. Use curl to POST


  1. cPost('l8200352367');
  2. /**
  3. * Use POST method in CURL to submit data
  4. *@param string $xml $xml data to be submitted
  5. */
  6. function cPost($phone){
  7. $curlPost = "mobileCode=$phone&userID=";
  8. $ ch = curl_init();//Initialize curl session, return a handle
  9. curl_setopt($ch, CURLOPT_URL, "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo");
  10. curl_setopt($ ch, CURLOPT_POST, 1);//When enabled, a regular POST request will be sent, the type is: application/x-www-form-urlencoded, just like the form submission
  11. curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
  12. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//Return the information obtained by curl_exec() in the form of a file stream instead of outputting it directly
  13. $res = curl_exec($ch);
  14. curl_close($ch);
  15. var_dump ($res);
  16. }
Copy code
Two kinds, web, php


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php image upload methodNext article:php image upload method