2011-05-16 55 views
0

我正在使用一個外部API webservice,它返回一個json輸出true或false。我訪問像使用我的Zend項目中的webservice

http://site.com/api/valid 

一個URL,它給了我這樣的事情,它看起來像JSON

"true" 

現在我手動訪問URL,但我想現在就做編程方式從在我的zend項目中。我應該用什麼來正確得到結果

回答

1

有很多方法。最簡單的是使用file_get_contents()

$result = file_get_contents("http://site.com/api/valid"); 
// if result is truly json 
// data will be 
// array(0 => true) 
$data = json_decode($result); 

如果它是一個流行的web服務。可能會有一個爲它編寫的庫。這是首選,因爲它可以處理錯誤情況和角落情況。它首先在其周圍。

1

聽起來像你需要一種方法,可以抓住端點。嗯,有很多方法,但既然你已經使用Zend,你不妨閱讀了關於http://framework.zend.com/manual/1.11/en/zend.http.client.adapters.html

這裏有一個靜態方法:

static function curl($url, $method, $params = array()){ 
      $client = new Zend_Http_Client($url); 
      if($method == "POST"){ 
       $client->setParameterPOST($params); 
      }else{ 
       $client->setParameterGet($params); 
      } 
      $response = $client->request($method); 
      return $response->getBody(); 
     } 

或者使用PHP的本地方法$響應=的file_get_contents($網址) ;

確保json_decode()您的回覆。

相關問題