分享经验,是为了让你少走弯路。————华伟君原创·技术博客
###在PHP中模拟post提交方式,调用JSON接口
在Jquery中我们可以很方便的使用$.ajax()方法来调用数据接口,获取数据,然后进行解析应用。 在PHP中,虽然没有类如$.ajax()的直接方法,通过接口获取数据,但是我们可以通过自定义的方式,编写调用接口的封装函数,来实现我们的post接口调用方式。
基本实现原理
/**
* 模拟post进行url请求
* @param string $url
* @param string $param
*/
function request_post($url = '', $param = '') {
if (empty($url) || empty($param)) {
return false;
}
$postUrl = $url;
$curlPost = $param;
$ch = curl_init(); //初始化curl
curl_setopt($ch, CURLOPT_URL,$postUrl); //抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0); //设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($ch); //运行curl
curl_close($ch);
return $data;
}
多参数时具体调用实例,将post进行拼接
function testAction(){
$url = 'http://my.oschina.net/chinacion/blog';
$post_data['appid'] = '10';
$post_data['appkey'] = 'cmbohpffXVR03nIpkkQXaAA1Vf5nO4nQ';
$post_data['member_name'] = 'zsjs123';
$post_data['password'] = '123456';
$post_data['email'] = 'zsjs123@126.com';
$o = "";
foreach ( $post_data as $k => $v )
{
$o.= "$k=" . urlencode( $v ). "&" ;
}
$post_data = substr($o,0,-1); //去掉结尾的“&”
$res = $this->request_post($url, $post_data);
print_r($res);
}
这样就提交请求,并且获取请求结果了。一般返回的结果是json格式的。
也可以改造成下面的方式,将拼接也封装起来。
/**
* 模拟post进行url请求
* @param string $url
* @param array $post_data
*/
function request_post($url = '', $post_data = array()) {
if (empty($url) || empty($post_data)) {
return false;
}
$o = "";
foreach ( $post_data as $k => $v )
{
$o.= "$k=" . urlencode( $v ). "&" ;
}
$post_data = substr($o,0,-1);
$postUrl = $url;
$curlPost = $post_data;
$ch = curl_init();//初始化curl
curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($ch);//运行curl
curl_close($ch);
return $data;
}
这样调用的时候就更简洁了。
function testAction(){
$url = 'http://my.oschina.net/chinacion/blog';
$post_data['appid'] = '10';
$post_data['appkey'] = 'cmbohpffXVR03nIpkkQXaAA1Vf5nO4nQ';
$post_data['member_name'] = 'zsjs124';
$post_data['password'] = '123456';
$post_data['email'] = 'zsjs124@126.com';
//$post_data = array();
$res = $this->request_post($url, $post_data);
print_r($res);
}
PHP中对获得的json结果进行解析,可以直接使用函数json_decode
json_decode函数详解: json_decode — 对 JSON 格式的字符串进行编码 语法: mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] ) 参数详解: $json:待解码的 json string 格式的字符串,这个函数仅能处理 UTF-8 编码的数据。 $assoc:当该参数为 TRUE 时,将返回 array 而非 object 。 $depth:用户指定的递归深度 $options:JSON的位掩码解码选项。目前只支持JSON_BIGINT_AS_STRING(默认是将整数做为浮点数)
** json_decode实例**
<?php
$json = '{"a":"php","b":"mysql","c":3}';
$json_Class=json_decode($json);
$json_Array=json_decode($json, true);
print_r($json_Class);
print_r($json_Array);
?>
程序输出:
stdClass Object (
[a] => php
[b] => mysql
[c] => 3 )
Array (
[a] => php
[b] => mysql
[c] => 3 )
最后 少侠,看到这儿了,举手之劳点个赞噻~