2010-04-25 48 views

回答

5

使用curl功能:

http://php.net/manual/en/book.curl.php

假設你使用的是GET請求連接到一個RESTful API:

$url = "http://the-api-you-want/?the-args=your-args&another-arg=another-arg"; 
$ch = curl_init(); // start CURL 
curl_setopt($ch, CURLOPT_URL, $url); // set your URL 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // get the response as a variable 
$json = curl_exec($ch); // connect and get your JSON response 
curl_close($ch); 

然後,您可以使用PHP的json_decode的響應,如果這是你想要做什麼。

另一種選擇是使用Drupal的drupal_http_request功能http://api.drupal.org/api/function/drupal_http_request/6

+0

這隻適用於REST風格的服務:p – 2010-04-25 10:00:24

+0

您可以多解釋一下。我是CURL的新手,我想使用json進行數據傳輸 – Warrior 2010-04-25 10:00:56

+0

您的意思是您希望獲得JSON響應,還是希望在查詢中發送JSON? – Finbarr 2010-04-25 10:03:22

0

我會reccommend你在你的問題更具體。你認爲什麼類型的Web服務?

如果您使用Rest Webservices,我可以推薦Zend Framework隨附的Zend_Rest_Client。我認爲Zend Framework還提供了SOAP服務的Stuff。

+0

我正在使用drupal cms – Warrior 2010-04-25 10:04:15

0

使用curl或Zend Framework中的Zend_Http_Client庫(您不需要整個Zend Framework來使用庫)。如果您正在調用的服務正在發送JSON響應,那麼您必須使用json_decode以PHP解析它。