我们的客户端给了我一个REST API,我需要对它进行PHP调用。但事实上,API提供的文档非常有限,所以我真的不知道如何调用该服务。
我试着谷歌它,但唯一出现的是一个已经过期的雅虎!关于如何调用服务的教程。不要提及标题或任何深入的信息。
是否有关于如何调用REST API的适当信息或相关文档?因为即使在w3学校中,它们也只描述SOAP方法。在PHP中创建API的其他选项有哪些?
我们的客户端给了我一个REST API,我需要对它进行PHP调用。但事实上,API提供的文档非常有限,所以我真的不知道如何调用该服务。
我试着谷歌它,但唯一出现的是一个已经过期的雅虎!关于如何调用服务的教程。不要提及标题或任何深入的信息。
是否有关于如何调用REST API的适当信息或相关文档?因为即使在w3学校中,它们也只描述SOAP方法。在PHP中创建API的其他选项有哪些?
当前回答
如果你有一个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
其他回答
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);
如果您正在使用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方法。
如果你愿意使用第三方工具,你可以看看这个: 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();
使用HTTPFUL
Httpful是一个简单的,可链接的,可读的PHP库,旨在使HTTP说话正常。它让开发人员专注于与api的交互,而不是筛选curl set_opt页面,是一个理想的PHP REST客户端。
Httpful包括……
可读HTTP方法支持(GET, PUT, POST, DELETE, HEAD和OPTIONS) 自定义标题 自动“智能”解析 自动负载序列化 基本认证 客户端证书认证 请求“模板”
Ex.
发送一个GET请求。获得自动解析的JSON响应。
库注意到响应中的JSON Content-Type,并自动将响应解析为原生PHP对象。
$uri = "https://www.googleapis.com/freebase/v1/mqlread?query=%7B%22type%22:%22/music/artist%22%2C%22name%22:%22The%20Dead%20Weather%22%2C%22album%22:%5B%5D%7D";
$response = \Httpful\Request::get($uri)->send();
echo 'The Dead Weather has ' . count($response->body->result->album) . " albums.\n";
如果你有一个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