php 获取远程资源,发送数据

来源:互联网 发布:centos打开终端快捷键 编辑:程序博客网 时间:2024/06/10 05:35
<?php header("content-type:text/html;charset=utf-8");/*我加了很多PHP的技术群,经常有人在群里问 怎么抓取网页内容,模拟客户端发送post数据等等 后来抽空我也研究了下做出如下整理*/// 模拟数据$data = array("name"=>"张三","age"=>"16","id"=>"12","addres"=>"meizhoudao");$body = http_build_query($data);// 方法一  内置函数 $data = file_get_contents("http://my.note.com/service.php");dump($data);// 方法二  内置函数 GET$data = file_get_contents("http://my.note.com/service.php?".$body);dump(json_to_array($data));// 方法三  内置函数 POST$opts = array('http' => array('method'  => 'POST','header'  => 'Content-type: application/x-www-form-urlencoded','content' => $body));$context = stream_context_create($opts);$result = file_get_contents('http://my.note.com/service.php', false, $context);dump(json_to_array($data));// 方法四  curl$c = curl_init('http://my.note.com/service.php?');curl_setopt($c, CURLOPT_RETURNTRANSFER, true); $page = curl_exec($c);curl_close($c);dump($page);// 方法五  curl get$c = curl_init('http://my.note.com/service.php?'.$body);curl_setopt($c, CURLOPT_RETURNTRANSFER, true); $page = curl_exec($c);curl_close($c);// 解析返回值dump(json_to_array($page));// 方法六  curl post$c = curl_init('http://my.note.com/service.php');curl_setopt($c, CURLOPT_POST, true);curl_setopt($c, CURLOPT_POSTFIELDS, $body);curl_setopt($c, CURLOPT_RETURNTRANSFER, true);$page = curl_exec($c);curl_close($c);// 解析返回值dump(json_to_array($page));// 方法七 fopen$handle = fopen ('http://my.note.com/service.php', "rb");$contents = "";do{$data = fread($handle, 8192);if (strlen($data) == 0) {break;}$contents .= $data;}while(true);fclose ($handle);// 解析返回值dump($contents);// 方法八 fopen GET$handle = fopen ('http://my.note.com/service.php?'.$body, "rb");$contents = "";do{$data = fread($handle, 8192);if (strlen($data) == 0) {break;}$contents .= $data;}while(true);fclose ($handle);// 解析返回值dump(json_to_array($contents));function dump($obj){echo "<pre>";var_dump($obj);echo "<hr>";}function json_to_array($str){return json_decode($str,true);}


serivce.php

<?php//  如果没有带参数  直接返回信息if(empty($_GET) and empty($_POST)){die("欢迎访问我的网站!");}echo json_encode($_REQUEST);die();



0 0
原创粉丝点击