php調用web services兩種方法soap和curl

jopen 9年前發布 | 4K 次閱讀 PHP

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

一、使用soap調用

<?php
 //服務器支持soap擴展:
  /*Example 1:
  $client = new SoapClient("http://fy.webxml.com.cn/webservices/EnglishChinese.asmx?wsdl");
  $parameters = array("wordKey"=>"test");
  //中英文雙向翻譯返回數據:數組
  $result = $client->TranslatorString($parameters);
   echo "<pre>";
  print_r($result->TranslatorStringResult)."<br />";
   echo "</pre>";
//中英文雙向翻譯返回數組含句子例子:
  $result1 = $client->Translator($parameters);
  echo "<pre>";
  print_r($result1->TranslatorResult)."<br />";
  echo "</pre>";
//獲得候選詞:
  $result2 = $client->SuggestWord($parameters);
  echo "<pre>";
  print_r($result2->SuggestWordResult)."<br />";
  echo "</pre>";
//獲得朗讀MP3字節流,返回數據:字節數組 Byte[]
  $result3 = $client->GetMp3($parameters);
  echo "<pre>";
  print_r($result3)."<br />";
  echo "</pre>";
  */


 /*Example2:

  $client = new SoapClient("http://webservice.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl");
  $param = array('theIpAddress'=>'202.96.134.33');
  $result = $client->getCountryCityByIp($param);
  echo "<pre>";
  print_r($result->getCountryCityByIpResult);
  echo "</pre>";

  $result1 = $client->getGeoIPContext($param);
  echo "<pre>";
  print_r($result1);
  echo "</pre>";

  $result2 = $client->getVersionTime(
  );
  echo "<pre>";
  print_r($result2);
  echo "</pre>";

  */
  //Example3:

  $client = new SoapClient("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl");
  //獲得國內手機號碼歸屬地省份、地區和手機卡類型信息
  $parm=array('mobileCode'=>'1367007','userID'=>'');
  $result=$client->getMobileCodeInfo($parm);
  echo ($result->getMobileCodeInfoResult)."<br>";
  //獲得國內手機號碼歸屬地數據庫信息
  $result1 = $client->getDatabaseInfo($parm);
  print_r($result1)."<br>";

// 獲取SOAP類型列表(Returns list of SOAP types )
    echo '<pre>';
    print_r($client->__getTypes ()) ;
    echo '</pre>';

// 獲取webservice提供的函數
    echo '<pre>';
    print_r($client->__getFunctions ()) ;
    echo '</pre>';    
//服務器不支持soap擴展的情況下,可引入網上開源的類庫   
?>


二、使用curl中POST


<?php
     cPost('l8200352367');
     /**
         * 使用CURL中POST方式提交數據
         *@param string $xml 要提交的$xml數據
         */ 
     function cPost($phone){
            $curlPost = "mobileCode=$phone&userID=";
            $ch = curl_init();//初始化curl會話,返回一個句柄
            curl_setopt($ch, CURLOPT_URL, "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo");
            curl_setopt($ch, CURLOPT_POST, 1);//啟用時會發送一個常規的POST請求,類型為:application/x-www-form-urlencoded,就像表單提交的一樣
            curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1);//將 curl_exec() 獲取的信息以文件流的形式返回,而不是直接輸出
            $res = curl_exec($ch);
            curl_close($ch);
            var_dump($res);
         }
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!