1 file_get_contents get

$opts = array(
  'http'=>array(
    'method'=>"GET",
    'timeout'=>10,
  )
);
$context = stream_context_create($opts);
$html =file_get_contents('http://www.example.com', false, $context);
echo $html;

1.2 file_get_contents post

<?php
$data = array("name" => 'test_name',"content" => 'test_con');
$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" .
"Cookie: foo=bar\r\n" .
"\r\n",
'content' => $data,
)
);
$cxContext = stream_context_create($opts);
$sFile = file_get_contents("http://127.0.0.1/reponse.php", false, $cxContext);
echo $sFile;
?>

2 使用curl,get获取数据

<?php
$url = 'http://www.example.com';
//初始化一个 cURL 对象
$ch = curl_init();
//设置你需要抓取的URL
curl_setopt($ch, CURLOPT_URL, $url);
// 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//是否获得跳转后的页面
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
?>

2.2 使用curl。post获取数据

<?php
function curl_post($url, $arr_data){
$post_data = http_build_query($url_data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFLELDS, $post_data);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
}
$arr_post = array(
'name'=>'test_name',
'age' => 1
);
curl_post("http://www.explame.com/", $arr_post);
?>

最新文章

  1. JS学习笔记(不断更新)
  2. 通过rpc访问比特币核心钱包
  3. python之redis
  4. windows下svn自动更新
  5. eclipse项目持续报红解决
  6. oracle中的针对该库的表
  7. keditor_php图片上传
  8. 为duilib的MenuDemo增加消息响应,优化代码和显示效果
  9. 利用Azure Automation实现云端自动化运维(2)
  10. 原创js自动补全---auotocomplete
  11. JavaScript基础知识(对象、函数与对象)
  12. Gitlab权限管理-issue管理[六]
  13. DOM常用外部插入方法与区别
  14. Centos 7 Puppet之foreman介绍安装测试
  15. java内存问题排查及分析
  16. Tell Me About Yourself - Best Answers and Examples
  17. Vmware ESXi 的虚拟机的开机自启动
  18. cocos源码分析--Sprite绘图原理
  19. Linux 设置IP地址,并能连接外网
  20. Linux-TCP 出现 RST 的几种情况

热门文章

  1. javaWeb核心技术之分页和条件
  2. Linux实现树莓派3B的国密SM9算法交叉编译——(一)环境部署、简单测试与eclipse工程项目测试
  3. glog与gflags的windows编译
  4. java 实体类 时间格式字段注解
  5. PyCharm 上传项目到码云托管平台
  6. Linux shell cut 命令详解
  7. UVA 10881 Piotr&#39;s Ants(模拟)
  8. JAVA 爬虫框架webmagic 初步使用Demo
  9. mapreduce课上实验
  10. MyBatis XML常用配置