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 框 ...
随机推荐
- HttpClient 学习整理【转】
转自 http://www.blogjava.net/Alpha/archive/2007/01/22/95216.html HttpClient 是我最近想研究的东西,以前想过的一些应用没能有很好的 ...
- redis集群节点宕机
redis集群是有很多个redis一起工作,那么就需要这个集群不是那么容易挂掉,所以呢,理论上就应该给集群中的每个节点至少一个备用的redis服务.这个备用的redis称为从节点(slave). 1. ...
- ELk之使用kibana展示访问IP地图
参考文档:http://blog.51cto.com/ls40905250/1915280 https://blog.csdn.net/zsjwish/article/details/79792212 ...
- 使用find命令按条件查找多个文件并且拷贝至指定目录
命令格式如下 find / \( -name "*.war" -o -name "*.jar" \) | xargs -i cp {} ${wardir} 当需 ...
- 《机器学习实战》中的程序清单2-1 k近邻算法(kNN)classify0都做了什么
from numpy import * import operator import matplotlib import matplotlib.pyplot as plt from imp impor ...
- python面向对象高级:定制类
Python的class中还有许多这样有特殊用途的函数,可以帮助我们定制类. 比如: __str__ 与__repr____iter____getitem____call__ __str__ 与__r ...
- gulp-webserver
gulp-webserver是开启服务器,通常和gulp-livereload结合使用.而这两个结合使用效果,几乎类似browser-Sync.下面是gulp-webserver和gulp-liver ...
- oracle(十)临时表
1.临时表的特点 (1)多用户操作的独立性:对于使用同一张临时表的不同用户,oracle都会分配一个独立的 Temp Segment,这样就避免了多个用户在对同一张临时表操作时 发生交叉,从而保证了多 ...
- excel用法
1:求大于某一值的个数:使用COUNTIF(区间,标准) 要大写 =COUNTIF(B2:B48,">=95") 2:求某一区间的个数用:大区间个数减小区间个数 =CO ...
- for与while的特点及其if在什么情况下使用情况
for和while的特点: 什么时候使用循环结构呢? 1:当对某些代码执行很多次时,使用循环结构完成. 2:当对一个条件进行一次判断时,可以使用if语句. 3:当对一个条件进行多次判断时,可以使用wh ...