引用:http://zhidao.baidu.com/question/454935450.html&__bd_tkn__=67bd5d3a742a8b244e09a86fb8b824aa950c9efd8078338d51fed8133ea5c69d362ad36bb4bcda3b39bb3949f6bbe47087ac3af56e60b1f4e7eb60157c59f93b9e62adfd5e0f03de01252778a636bc0c4a739c050b5fbd8ed149437d742a3220cb647f3449c2aba89e0ef9accbdc8c0bc23026f14aa0

如:
http://localhost/operate.php?act=get_user_list&type=json

在这里operate.php相当于一个接口,其中get_user_list 是一个API(获取用户列表),讲求返回的数据类型为JSON格式。

你只需要在你PHP代码中执行这条链接他就会返回。
GET方式的直接使用 
$file_contents = file_get_contents('http://localhost/operate.php?act=get_user_list&type=json')

POST方式得用下面的(需要开启PHP curl支持)。 
$url = 'http://localhost/operate.php?act=get_user_list&type=json';
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 10 );
curl_setopt ( $ch, CURLOPT_POST, 1 ); //启用POST提交
$file_contents = curl_exec ( $ch );
curl_close ( $ch );

方法1:用file_get_contents以get方式获取内容 <?php $url=’http://www.zhoz.com/’; $html=file_get_contents($url); //print_r($http_response_header); ec($html); printhr(); printarr($http_response_header); printhr(); ?> 
 
方法2:用fopen打开url,以get方式获取内容 我觉得这个方法比较常用。 <?php $fp=fopen($url,‘r’); printarr(stream_get_meta_data($fp)); printhr(); while(!feof($fp)){ $result.=fgets($fp,1024); } echo"urlbody:$result"; printhr(); fclose($fp); ?> 
 
方法3:用file_get_contents函数,以post方式获取url <?php $data=array(’foo’=>‘bar’); $data=http_build_query($data); $opts=array( ‘http’=>array( ‘method’=>‘POST’, ‘header’=>"Content-type:application/x-www-form-urlencoded\r\n". "Content-Length:".strlen($data)."\r\n", ‘content’=>$data ), ); $context=stream_context_create($opts); $html=file_get_contents(’http://localhost/e/admin/test.html’,false,$context); echo$html; ?> 
 
方法4:用fsockopen函数打开url,以get方式获取完整的数据,包括header和body <?php functionget_url($url,$cookie=false){ $url=parse_url($url); $query=$url[path]."?".$url[query]; ec("Query:".$query); $fp=fsockopen($url[host],$url[port]?$url[port]:80,$errno,$errstr,30); if(!$fp){ returnfalse; }else{ $request="GET$queryHTTP/1.1\r\n"; $request.="Host:$url[host]\r\n"; $request.="Connection:Close\r\n"; if($cookie)$request.="Cookie:$cookie\n"; $request.="\r\n"; fwrite($fp,$request); while(!@feof($fp)){ $result.=@fgets($fp,1024); } fclose($fp); return$result; } } //获取url的html部分,去掉header functionGetUrlHTML($url,$cookie=false){ $rowdata=get_url($url,$cookie); if($rowdata) { $body=stristr($rowdata,"\r\n\r\n"); $body=substr($body,4,strlen($body)); return$body; } returnfalse; } ?> 
 
方法5:用fsockopen函数打开url,以POST方式获取完整的数据,包括header和body <?php functionHTTP_Post($URL,$data,$cookie,$referrer=""){ //parsingthegivenURL $URL_Info=parse_url($URL); //Buildingreferrer if($referrer=="")//ifnotgivenusethisscriptasreferrer $referrer="111"; //makingstringfrom$data foreach($dataas$key=>$value) $values[]="$key=".urlencode($value); $data_string=implode("&",$values); //Findoutwhichportisneeded–ifnotgivenusestandard(=80) if(!isset($URL_Info["port"])) $URL_Info["port"]=80; //buildingPOST-request: $request.="POST".$URL_Info["path"]."HTTP/1.1\n"; $request.="Host:".$URL_Info["host"]."\n"; $request.="Referer:$referer\n"; $request.="Content-type:application/x-www-form-urlencoded\n"; $request.="Content-length:".strlen($data_string)."\n"; $request.="Connection:close\n"; $request.="Cookie:$cookie\n"; $request.="\n"; $request.=$data_string."\n"; $fp=fsockopen($URL_Info["host"],$URL_Info["port"]); fputs($fp,$request); while(!feof($fp)){ $result.=fgets($fp,1024); } fclose($fp); return$result; } printhr(); ?> 
 
方法6:使用curl库,使用curl库之前,你可能需要查看一下php.ini,查看是否已经打开了curl扩展 <?php $ch=curl_init(); $timeout=5; curl_setopt($ch,CURLOPT_URL,‘http://www.zhoz.com/’); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); $file_contents=curl_exec($ch); curl_close($ch); echo$file_contents; ?> 关于curl库: curl官方网站http://curl.haxx.se/ curl是使用URL语法的传送文件工具,支持FTP、FTPS、HTTPHTPPSSCPSFTPTFTPTELNETDICTFILE和LDAP。curl支持SSL证书、HTTPPOST、HTTPPUT、FTP上传,kerberos、基于HTT格式的上传、代理、cookie、用户+口令证明、文件传送恢复、http代理通道和大量其他有用的技巧,最近热门的SNS中也用到这个方法,取得MSN上的好友列表等,应用还是挺多的。只不过需要组件支持,开启方法我的技术圈中有说明:http://o.zhoz.com/ <?php functionprintarr(array$arr) { echo"
Rowfieldcount:".count($arr)."
"; foreach($arras$key=>$value) { echo"$key=$value
"; } } ?>
 
 
 
 
 /**
* 模拟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;
}

这是方法,

下面是具体的调用案例。

    function testAction(){
$url = 'http://mobile.jschina.com.cn/jschina/register.php';
$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是拼接出来的。

也可以改造成下面的方式。

/**
* 模拟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://mobile.jschina.com.cn/jschina/register.php';
$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中调用接口的更多相关文章

  1. 在网页程序或Java程序中调用接口实现短信猫收发短信的解决方案

    方案特点: 在网页程序或Java程序中调用接口实现短信猫收发短信的解决方案,简化软件开发流程,减少各应用系统相同模块的重复开发工作,提高系统稳定性和可靠性. 基于HTTP协议的开发接口 使用特点在网页 ...

  2. api 接口开发理论 在php中调用接口以及编写接口

    如: http://localhost/openUser.php?act=get_user_list&type=json 在这里openUser.php相当于一个接口,其中get_user_l ...

  3. android 中调用接口发送短信

    android中可以通过两种方式发送短信 第一:调用系统短信接口直接发送短信:主要代码如下: //直接调用短信接口发短信 SmsManager smsManager = SmsManager.getD ...

  4. 在PHP中调用接口

    引用:http://zhidao.baidu.com/question/454935450.html&__bd_tkn__=67bd5d3a742a8b244e09a86fb8b824aa95 ...

  5. WebApi接口 - 如何在应用中调用webapi接口

    很高兴能再次和大家分享webapi接口的相关文章,本篇将要讲解的是如何在应用中调用webapi接口:对于大部分做内部管理系统及类似系统的朋友来说很少会去调用别人的接口,因此可能在这方面存在一些困惑,希 ...

  6. 在C代码中调用C++接口

    一 在C源文件中调用C++封装的接口 例如: 要想在A.c文件中,调用生命在B.h,实现在B.cpp中的接口bool getMAC(char *mac_addr); 其实现方法 B.cpp 如下: / ...

  7. AutoCAD.NET 不使用P/Invoke方式调用acad.exe或accore.dll中的接口(如acedCommand、acedPostCommand等)

    使用C#进行AutoCAD二次开发,有时候由于C#接口不够完善,或者低版本AutoCAD中的接口缺少,有些工作不能直接通过C#接口来实现,所以需要通过P/Invoke的方式调用AutoCAD的其他DL ...

  8. json格式数据,将数据库中查询的结果转换为json, 然后调用接口的方式返回json(方式一)

    调用接口,无非也就是打开链接 读取流 将结果以流的形式输出 将查询结果以json返回,无非就是将查询到的结果转换成jsonObject ================================ ...

  9. 在php中调用以及编写接口(转)

    如: http://localhost/openUser.php?act=get_user_list&type=json 在这里openUser.php相当于一个接口,其中get_user_l ...

随机推荐

  1. 解决apt 依赖破损的问题

    在 安装 python3 / python2 共存的环境时, 不知道做了什么, 导致 apt 依赖故障 odoo@sy-odoo-08:~$ sudo apt-get remove apport 正在 ...

  2. Java 使用StringBuffer注意

    Stringbuffer使用注意   问题背景: 模拟客户端使用Socket请求服务器核心系统,核心系统正常响应,内容较大,近2715KB,大于2.6M多. 使用指定编码GBK来接收响应内容到过程中没 ...

  3. ubantu 下 修改mysql 默认编码

    启动mysql后,以root登录mysql root@Eadgar-virtual-machine:~# mysql -uroot -proot mysql> show variables li ...

  4. 【Android】百度地图自定义弹出窗口

    我们使用百度地图的时候,点击地图上的Marker,会弹出一个该地点详细信息的窗口,如下左图所示,有时候,我们希望自己定义这个弹出窗口的内容,或者,干脆用自己的数据来构造这样的弹出窗口,但是,在百度地图 ...

  5. PHPCMS替换主页、列表页、内容页

    利用phpcms制作企业站,首先要将静态的企业主页替换成后台可编辑的动态主页. 在phpcms/install_package/phpcms/templates新建一个英文文件夹 在此文件夹下在创建一 ...

  6. mongo-connector导入数据到Es

    要求 基于mongo-connector同步数据,必须要求mongodb为复制集架构,原因是此插件是基于oplog操作记录进行数据同步的:而oplog可以说是Mongodb Replication的纽 ...

  7. java基本类型和包装类的区别(转)

    int 是基本类型,直接存数值 Integer是类,产生对象时用一个引用指向这个对象 Java把内存划分成两种:一种是栈内存,另一种是堆内存 在函数中定义的一些基本类型的变量和对象的引用变量都是在函数 ...

  8. 九度OJ 1035:找出直系亲属 (二叉树、递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2380 解决:934 题目描述:     如果A,B是C的父母亲,则A,B是C的parent,C是A,B的child,如果A,B是C的(外) ...

  9. 响应式布局【3】 --- bootstrap

    本片文章主要讲解Bootstrap中如何实现响应式布局的. 参考资料&内容来源: https://code.ziqiangxuetang.com/bootstrap/bootstrap-gri ...

  10. php总结2——php中的变量、数据类型及转换、运算符、流程控制中的分支结构

    2.1  php中的变量: 定义变量:$变量名称=值: 变量名称:$开头    $之后的第一位必须是字母    $第二位之后可以是字母.数字或者是下划线.习惯上变量名称有实际含义,第二个单词后首字母大 ...