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