PHP发送HTTP请求的6种方法
方法1: 用 file_get_contents 以get方式获取内容:
<?php
$url = 'https://wenda.shukaiming.com/';
echo file_get_contents($url);
?>
2
4
方法2: 用fopen打开url, 以get方式获取内容:
<?php
//r标识read,即标识只读
$fp = fopen($url, 'r');
stream_get_meta_data($fp);
while (!feof($fp)) {
$body.= fgets($fp, 1024);
}
echo $body;
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-urlencodedrn' . 'Content-Length: ' . strlen($data) . '\r\n', 'content' => $data));
$context = stream_context_create($opts);
$html = file_get_contents('https://wenda.shukaming.com', false, $context);
echo $html;
?>
方法4:用fsockopen函数打开url,以get方式获取完整的数据,包括header和body,fsockopen需要 PHP.ini 中 allow_url_fopen 选项开启
<?php
function get_url($url, $cookie = false) {
$url = parse_url($url);
$query = $url[path] . ” ? ” . $url[query];
echo $query;
$fp = fsockopen($url[host], $url[port] ? $url[port] : 80, $errno, $errstr, 30);
if (!$fp) {
return false;
} else {
$request = "GET $query HTTP/1.1\r\n";
$request.= "Host: $url[host]\r\n";
$request.= "Connection: Close\r\n";
if ($cookie) $request.= "Cookie: $cookien";
$request.= "\r\n";
fwrite($fp, $request);
while (!@feof($fp)) {
$result.= @fgets($fp, 1024);
}
fclose($fp);
return $result;
}
}
//获取url的html部分,去掉header
function GetUrlHTML($url, $cookie = false) {
$rowdata = get_url($url, $cookie);
if ($rowdata) {
$body = stristr($rowdata, ”rnrn”);
$body = substr($body, 4, strlen($body));
return $body;
}
return false;
}
?>
方法5:用fsockopen函数打开url,以POST方式获取完整的数据,包括header和body
<?php
function HTTP_Post($URL, $data, $cookie, $referrer = "") {
// parsing the given URL
$URL_Info = parse_url($URL);
// Building referrer
if ($referrer == "") // if not given use this script as referrer
$referrer = "111";
// making string from $data
foreach ($data as $key => $value) $values[] = "$key=" . urlencode($value);
$data_string = implode("&", $values);
// Find out which port is needed – if not given use standard (=80)
if (!isset($URL_Info["port"])) $URL_Info["port"] = 80;
// building POST-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-urlencodedn";
$request.= "Content-length: " . strlen($data_string) . "\n";
$request.= "Connection: closen";
$request.= "Cookie: $cookien";
$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;
}
?>
方法6:使用curl库,使用curl库之前,可能需要查看一下php.ini是否已经打开了curl扩展
<?php
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, 'http://wenda.shukaiming.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;
?>
PHP发送HTTP请求的6种方法的更多相关文章
- php发送post请求的三种方法示例
本文分享下php发送post请求的三种方法与示例代码,分别使用curl.file_get_content.fsocket来实现post提交数据,大家做个参考. php发送post请求的三种方法,分别使 ...
- php发送post请求的三种方法
引用:http://blog.sjzycxx.cn/post/435/ 1.使用 file_get_contents() /** * 发送post请求 * @param string $url 请求地 ...
- php发送get、post请求的6种方法代码示例
本文主要展示了php发送get.post请求的6种方法的代码示例,分别为使用file_get_contents .fopen.fsockopen.curl来发送GET和POST请求,代码如下: 方法1 ...
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- (一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
(一)----使用HttpClient发送HTTP请求(通过get方法获取数据) 一.HTTP协议初探: HTTP(Hypertext Transfer Protocol)中文 “超文本传输协议”,是 ...
- 【MySQL】锁——查看当前数据库锁请求的三种方法 20
MySQL提供了查看当前数据库锁请求的三种方法:1. show full processlist命令 观察state和info列 2. show engine innodb status\G ...
- PHP 发送HTTP请求的几种方式
1.curl仍然是最好的HTTP库,没有之一. 可以解决任何复杂的应用场景中的HTTP 请求2. 文件流式的HTTP请求比较适合处理简单的HTTP POST/GET请求,但不适用于复杂的HTTP请求3 ...
- PHP发送HTTP请求的几种方式
转发:https://blog.tanteng.me/2017/07/php-curl-guzzlehttp/ 1)PHP开发中我们常用CURL 方式封装 HTTP请求,什么是CURL? CURL 是 ...
- ASP.NET MVC 实现AJAX跨域请求的两种方法
通常发送AJAX请求都是在本域内完成的,也就是向本域内的某个URL发送请求,完成部分页面的刷新.但有的时候需要向其它域发送AJAX请求,完成数据的加载,例如Google. 在ASP.NET MVC 框 ...
随机推荐
- 设置ubuntu默认中文字符
一. Ubuntu默认的中文字符编码 Ubuntu默认的中文字符编码为zh_CN.UTF-8,这个可以在 /etc/environment中看到:sudo gedit /etc/environment ...
- HDU 1166 - 敌兵布阵 - [单点修改、区间查询zkw线段树]
题还是那个题:http://www.cnblogs.com/dilthey/p/6827959.html 不过我们今天换一种线段树实现来做这道题: 关于zkw线段树的讲解:https://zhuanl ...
- linux:使用apt、dpkg工具安装软件
先总结一下安装软件常用命令: % apt sudo apt install xxxx sudo apt list xxxx % dpkg安装deb文件 sudo dpkg -i xxxx.deb 学习 ...
- Cache replacement policies 缓存实现算法
Cache replacement policies - Wikipedia https://en.wikipedia.org/wiki/Cache_replacement_policies Cach ...
- I Hate It---hdu1754线段树
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1754 和上一题一样是模板题,就是那道题求得是和,这道求得是最大值: #include<iostrea ...
- rem布局,flexible.js
//author:caibaojian //website:http://caibaojian.com //weibo:http:weibo.com/kujian //这段js的最后面有两个参数记得要 ...
- 深入浅出 TCP/IP 协议栈
写的不错:http://www.cnblogs.com/onepixel/p/7092302.html#3899256
- jquery css样式
1.单一样式 $('.list li').css( "float", "none"); 2. 多个样式 $('.list li').css( { ...
- 【剑指offer】 二叉树中和为某一值的路径
一.题目: 输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径.(注意: 在返回值的list中,数组长度 ...
- 2.深度学习中的batch_size的理解
Batch_Size(批尺寸)是机器学习中一个重要参数,涉及诸多矛盾,下面逐一展开. 首先,为什么需要有 Batch_Size 这个参数? Batch 的选择,首先决定的是下降的方向.如果数据集比较小 ...