我们的客户端给了我一个REST API,我需要对它进行PHP调用。但事实上,API提供的文档非常有限,所以我真的不知道如何调用该服务。
我试着谷歌它,但唯一出现的是一个已经过期的雅虎!关于如何调用服务的教程。不要提及标题或任何深入的信息。
是否有关于如何调用REST API的适当信息或相关文档?因为即使在w3学校中,它们也只描述SOAP方法。在PHP中创建API的其他选项有哪些?
我们的客户端给了我一个REST API,我需要对它进行PHP调用。但事实上,API提供的文档非常有限,所以我真的不知道如何调用该服务。
我试着谷歌它,但唯一出现的是一个已经过期的雅虎!关于如何调用服务的教程。不要提及标题或任何深入的信息。
是否有关于如何调用REST API的适当信息或相关文档?因为即使在w3学校中,它们也只描述SOAP方法。在PHP中创建API的其他选项有哪些?
当前回答
CURL是最简单的方法。这里有一个简单的调用
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "THE URL TO THE SERVICE");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, POST DATA);
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
其他回答
您可以使用file_get_contents来发出任何http POST/PUT/DELETE/OPTIONS/HEAD方法,除了函数名所示的GET方法之外。
如何在PHP中使用file_get_contents发布数据?
如果您正在使用Symfony,有一个很棒的rest客户端包,它甚至包含了所有~100个异常,并抛出它们,而不是返回一些毫无意义的错误代码+消息。
你真的应该检查一下: https://github.com/CircleOfNice/CiRestClientBundle
我喜欢它的界面:
try {
$restClient = new RestClient();
$response = $restClient->get('http://www.someUrl.com');
$statusCode = $response->getStatusCode();
$content = $response->getContent();
} catch(OperationTimedOutException $e) {
// do something
}
适用于所有http方法。
正如@Christoph Winkler提到的,这是实现它的基类:
curl_helper.php
// This class has all the necessary code for making API calls thru curl library
class CurlHelper {
// This method will perform an action/method thru HTTP/API calls
// Parameter description:
// Method= POST, PUT, GET etc
// Data= array("param" => "value") ==> index.php?param=value
public static function perform_http_request($method, $url, $data = false)
{
$curl = curl_init();
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// Optional Authentication:
//curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
//curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
}
然后你就可以包含这个文件并使用它,例如:any.php
require_once("curl_helper.php");
...
$action = "GET";
$url = "api.server.com/model"
echo "Trying to reach ...";
echo $url;
$parameters = array("param" => "value");
$result = CurlHelper::perform_http_request($action, $url, $parameters);
echo print_r($result)
如果你有一个url并且你的php支持它,你可以调用file_get_contents:
$response = file_get_contents('http://example.com/path/to/api/call?param1=5');
如果$response是JSON,使用json_decode将其转换为php数组:
$response = json_decode($response);
如果$response是XML,使用simple_xml类:
$response = new SimpleXMLElement($response);
http://sg2.php.net/manual/en/simplexml.examples-basic.php
如果你愿意使用第三方工具,你可以看看这个: https://github.com/CircleOfNice/DoctrineRestDriver
这是一种使用api的全新方式。
首先,你定义了一个实体,它定义了传入和输出数据的结构,并用数据源注释它:
/*
* @Entity
* @DataSource\Select("http://www.myApi.com/products/{id}")
* @DataSource\Insert("http://www.myApi.com/products")
* @DataSource\Select("http://www.myApi.com/products/update/{id}")
* @DataSource\Fetch("http://www.myApi.com/products")
* @DataSource\Delete("http://www.myApi.com/products/delete/{id}")
*/
class Product {
private $name;
public function setName($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
现在,与REST API进行通信非常容易:
$product = new Product();
$product->setName('test');
// sends an API request POST http://www.myApi.com/products ...
$em->persist($product);
$em->flush();
$product->setName('newName');
// sends an API request UPDATE http://www.myApi.com/products/update/1 ...
$em->flush();