前面博主写过一篇“ASP如何调用Delphi开发的DataSnap/Restful接口服务”的文章,有网友反映不知道如何使用PHP来调用。
其实PHP调用Restful接口服务比ASP更简单,写一个例子说明一下。
1、通用接口函数调用文件:testRestful.php
<?php
header("Content-Type:text/html;charset=utf-8");
?>
<?php
//数组转换成以'/'分隔的字符串
function http_build_query_delphi($arr)
{
//$arr = array('url'=>'http://www.jb51.net','name'=>'脚本之家','str'=>'PHP教程');
$result = '';
//foreach($arr as $key=>$value){
foreach($arr as $value){
$result .= '/'.$value;
}
if (strlen($result)>0)
$result = substr($result,1); //删除第一个"/"字符
return $result;
}
/**
* [http 调用接口函数]
* @Date 2016-07-11
* @Author GeorgeHao
* @param string $url [接口地址]
* @param array $params [数组]
* @param string $method [GETPOSTDELETEPUT]
* @param array $header [HTTP头信息]
* @param integer $timeout [超时时间]
* @return [type] [接口返回数据]
*/
function http($func, $params, $method = 'GET', $header = array(), $timeout = 5)
{
define('_SRV','http://localhost:8019/peci/rest/TServerMethods/'); //定义了一个常量,应从配置文件中读取
define('_KEY','91F11212729292F2136DADED3656B6CECECECD3145474855B6'); //应从配置文件中读取
$params["KeyNumber"] = _KEY;//增加一个Key键值和元素
$url = _SRV . $func;
// POST 提交方式的传入 $set_params 必须是字符串形式
$opts = array(
CURLOPT_TIMEOUT => $timeout,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTPHEADER => $header
);
/* 根据请求类型设置特定参数 */
switch (strtoupper($method)) {
case 'GET':
//通常$url=http://localhost/netpay/rest/TServerMethod1/GetXQList?xq=1&room=10110105
//$opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
$opts[CURLOPT_URL] = $url . '/' . http_build_query_delphi($params);
//Delphi生成的Restful服务接口函数调用时不是常见?x=值1&y=值2的方式,而是直接/值1/值2的方式,问号?也用/替代了。
//如:$url=http://localhost/netpay/rest/TServerMethod1/GetXQList/1/10110105
break;
case 'POST':
$params = http_build_query($params);
$opts[CURLOPT_URL] = $url;
$opts[CURLOPT_POST] = 1;
$opts[CURLOPT_POSTFIELDS] = $params;
break;
case 'DELETE':
$opts[CURLOPT_URL] = $url;
$opts[CURLOPT_HTTPHEADER] = array("X-HTTP-Method-Override: DELETE");
$opts[CURLOPT_CUSTOMREQUEST] = 'DELETE';
$opts[CURLOPT_POSTFIELDS] = $params;
break;
case 'PUT':
$opts[CURLOPT_URL] = $url;
$opts[CURLOPT_POST] = 0;
$opts[CURLOPT_CUSTOMREQUEST] = 'PUT';
$opts[CURLOPT_POSTFIELDS] = $params;
break;
default:
throw new Exception('不支持的请求方式!');
}
/* 初始化并执行curl请求 */
$ch = curl_init();
//如果网站需要安全验证,那么需要执行下面两条命令,否则就不需要。
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'username:password');
curl_setopt_array($ch, $opts);
$data = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
return $data;
}
?>
2、测试文件:test.php
<?php require("testRestful.php") ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Stict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1- strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang = "zh-CN"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <?php $func = "GetRoomInfo_ByRoom"; echo "调用接口方法:" . $func . "(xiaoqu_id,room,KeyNumber),结果如下:<hr>"; $params = array("xiaoqu_id" => "1", "room" => "10110105"); $result = http($func,$params); if ($result === false) echo "接口方法执行失败!"; else echo "接口方法执行成功!返回结果为:<p>" . $result; ?> </body> </html>执行结果为:
该接口方法其实查询出了后台数据库中的如下记录:
DEMEO文件打个包提供下载:http://download.csdn.net/download/xieyunc/9870904