Heim  >  Artikel  >  Backend-Entwicklung  >  php调用web services两种方法soap和curl

php调用web services两种方法soap和curl

WBOY
WBOYOriginal
2016-07-25 08:42:201235Durchsuche

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

一、使用soap调用

  1. //服务器支持soap扩展:
  2. /*Example 1:
  3. $client = new SoapClient("http://fy.webxml.com.cn/webservices/EnglishChinese.asmx?wsdl");
  4. $parameters = array("wordKey"=>"test");
  5. //中英文双向翻译返回数据:数组
  6. $result = $client->TranslatorString($parameters);
  7. echo "
    ";
  8. print_r($result->TranslatorStringResult)."
    ";
  9. echo "";
  10. //中英文双向翻译返回数组含句子例子:
  11. $result1 = $client->Translator($parameters);
  12. echo "
    ";
  13. print_r($result1->TranslatorResult)."
    ";
  14. echo "";
  15. //获得候选词:
  16. $result2 = $client->SuggestWord($parameters);
  17. echo "
    ";
  18. print_r($result2->SuggestWordResult)."
    ";
  19. echo "";
  20. //获得朗读MP3字节流,返回数据:字节数组 Byte[]
  21. $result3 = $client->GetMp3($parameters);
  22. echo "
    ";
  23. print_r($result3)."
    ";
  24. echo "";
  25. */
  26. /*Example2:
  27. $client = new SoapClient("http://webservice.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl");
  28. $param = array('theIpAddress'=>'202.96.134.33');
  29. $result = $client->getCountryCityByIp($param);
  30. echo "
    ";
  31. print_r($result->getCountryCityByIpResult);
  32. echo "";
  33. $result1 = $client->getGeoIPContext($param);
  34. echo "
    ";
  35. print_r($result1);
  36. echo "";
  37. $result2 = $client->getVersionTime(
  38. );
  39. echo "
    ";
  40. print_r($result2);
  41. echo "";
  42. */
  43. //Example3:
  44. $client = new SoapClient("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl");
  45. //获得国内手机号码归属地省份、地区和手机卡类型信息
  46. $parm=array('mobileCode'=>'1367007','userID'=>'');
  47. $result=$client->getMobileCodeInfo($parm);
  48. echo ($result->getMobileCodeInfoResult)."
    ";
  49. //获得国内手机号码归属地数据库信息
  50. $result1 = $client->getDatabaseInfo($parm);
  51. print_r($result1)."
    ";
  52. // 获取SOAP类型列表(Returns list of SOAP types )
  53. echo '
    ';
  54. print_r($client->__getTypes ()) ;
  55. echo '';
  56. // 获取webservice提供的函数
  57. echo '
    ';
  58. print_r($client->__getFunctions ()) ;
  59. echo '';
  60. //服务器不支持soap扩展的情况下,可引入网上开源的类库
  61. ?>
复制代码


二、使用curl中POST


  1. cPost('l8200352367');
  2. /**
  3. * 使用CURL中POST方式提交数据
  4. *@param string $xml 要提交的$xml数据
  5. */
  6. function cPost($phone){
  7. $curlPost = "mobileCode=$phone&userID=";
  8. $ch = curl_init();//初始化curl会话,返回一个句柄
  9. curl_setopt($ch, CURLOPT_URL, "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo");
  10. curl_setopt($ch, CURLOPT_POST, 1);//启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样
  11. curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
  12. curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1);//将 curl_exec() 获取的信息以文件流的形式返回,而不是直接输出
  13. $res = curl_exec($ch);
  14. curl_close($ch);
  15. var_dump($res);
  16. }
复制代码
两种, web, php


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn