4

使用PHP来调用REST API

 3 years ago
source link: https://yuguo.us/weblog/php-rest-api/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

表征状态转移(英文:Representational State Transfer,简称REST)是Roy Fielding博士在2000年他的博士论文中提出来的一种软件架构风格。

越来越多的公司开放了API,比如腾讯开放平台淘宝开放平台百度开放平台Google Developers等。开放的方式各不相同,有REST和SOAP两种。

REST很容易理解,而且只要是支持HTTP/HTTPS的客户端/服务器就支持它。你可以用HTTP GET方法来执行命令。所以,开发者们感受到的REST的优势是:开发简单、只需依托现有Web基础设施、以及学习成本低。

然而,SOAP作为一种古老的Web服务技术,短期内还不会退出历史舞台。

在我们第三方开发者的PHP应用程序中如果要使用REST API,主要分为两个步骤。一、生成请求。二、处理返回值。

一、生成请求

主要有三种方法来生成一个HTTP请求。第一种方法就是手动生成请求,使用PHP的header方法。这给了你最大的灵活性,但是需要更多的 编码。第二种方法是使用PHP内置的file_get_contents()方法或者file()/fopen()/fread()/fclose()方法,使用这种方法少了一点灵活性,但是代码量非常少。第三种方法是使用跟API配套的自定义Class,或者叫SDK。如果可以的话,尽量使用第三种方法,它是最方便的。

手动生成请求

手动生成请求只有在第一次处理的时候才有点棘手,以后的话可以调用方法直接得到结果。此外了解手动生成请求的过程也有助于理解REST和HTTP。

function callAPI($endpoint, $devkey, $action, $type, $keyword)

{

  $action = urlencode($action);

  $type = urlencode($type);

  $keyword = urlencode($keyword);

  $url = $endpoint . "?devkey=$devkey&action=$action&type=$type&keyword=$keyword";

  $url_info = parse_url($url);

  $host = $url_info['host'];

  $path = $url_info['path'] . "?" . $url_info[‘query'];

  $data = "";

  $fp=fsockopen($host, 80);

  fputs($fp, "POST ". $path . "HTTP/1.1\r\n");

  fputs($fp, "Host: ". $host ."\r\n");

  fputs($fp, "Accept: */*\r\n");

  fputs($fp, "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n");

  fputs($fp, "Connection: close\r\n");

  fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n");

  fputs($fp, "Content-Length: ". strlen($data) . "\r\n\r\n");

  fputs($fp, "$data");

  $response="";

  while(!feof($fp))

  {

    $response.=fgets($fp, 128);

  }

  fclose($fp);

  list($http_headers, $http_content)=explode("\r\n\r\n", $response);

  return $http_content;

}
function callAPIQuick($endpoint, $devkey, $action, $type, $keyword)

{

  $action = urlencode($action);

  $type = urlencode($type);

  $keyword = urlencode($keyword);

  $url = $endpoint . "?devkey=$devkey&action=$action&type=$type&keyword=$keyword";

  $response = @file_get_contents($url);

  return $response;

}

使用PHP的内置方法file_get_contents会让代码量少很多(也少了一些灵活性)。

 SDK方法

很多开放平台都会提供各语言的SDK下载,比如淘宝开放平台就提供了java、.net、PHP的SDK,并且还能够根据每个应用的API调用权限进行单独打包。

二、处理返回值

返回值要么是JSON格式,要么是XML格式。

如果是XML的话,可以使用PHP5的simplexml来解析。自从PHP 5.2,也已经默认加入了JSON格式的支持。

我写字的地方迁移到公众号啦~欢迎关注我的公众号:余果专栏


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK