如何从 php 调用 web 服务
最佳答案
使用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
关于php - 从 PHP 调用 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2707802/