方法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种方法的更多相关文章

  1. php发送post请求的三种方法示例

    本文分享下php发送post请求的三种方法与示例代码,分别使用curl.file_get_content.fsocket来实现post提交数据,大家做个参考. php发送post请求的三种方法,分别使 ...

  2. php发送post请求的三种方法

    引用:http://blog.sjzycxx.cn/post/435/ 1.使用 file_get_contents() /** * 发送post请求 * @param string $url 请求地 ...

  3. php发送get、post请求的6种方法代码示例

    本文主要展示了php发送get.post请求的6种方法的代码示例,分别为使用file_get_contents .fopen.fsockopen.curl来发送GET和POST请求,代码如下: 方法1 ...

  4. Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  5. (一)----使用HttpClient发送HTTP请求(通过get方法获取数据)

    (一)----使用HttpClient发送HTTP请求(通过get方法获取数据) 一.HTTP协议初探: HTTP(Hypertext Transfer Protocol)中文 “超文本传输协议”,是 ...

  6. 【MySQL】锁——查看当前数据库锁请求的三种方法 20

    MySQL提供了查看当前数据库锁请求的三种方法:1. show  full  processlist命令  观察state和info列 2. show engine  innodb status\G ...

  7. PHP 发送HTTP请求的几种方式

    1.curl仍然是最好的HTTP库,没有之一. 可以解决任何复杂的应用场景中的HTTP 请求2. 文件流式的HTTP请求比较适合处理简单的HTTP POST/GET请求,但不适用于复杂的HTTP请求3 ...

  8. PHP发送HTTP请求的几种方式

    转发:https://blog.tanteng.me/2017/07/php-curl-guzzlehttp/ 1)PHP开发中我们常用CURL 方式封装 HTTP请求,什么是CURL? CURL 是 ...

  9. ASP.NET MVC 实现AJAX跨域请求的两种方法

    通常发送AJAX请求都是在本域内完成的,也就是向本域内的某个URL发送请求,完成部分页面的刷新.但有的时候需要向其它域发送AJAX请求,完成数据的加载,例如Google. 在ASP.NET MVC 框 ...

随机推荐

  1. [分布式系统学习] 6.824 LEC2 RPC和线程 笔记

    6.824的课程通常是在课前让你做一些准备.一般来说是先读一篇论文,然后请你提一个问题,再请你回答一个问题.然后上课,然后布置Lab. 第二课的准备-Crawler 第二课的准备不是论文,是让你实现G ...

  2. Redis+Keepalived实现高可用

    使用redis哨兵可以在主服务器出现故障的时候自动切换主从,但是从服务器的IP不同于原主服务器的IP还需要在客户端手动修改IP才能生效 下面使用keepalived实现VIP自动漂移 keepaliv ...

  3. Oracle数据库中的优化方案

    来自: http://woainichenxueming.iteye.com/blog/726541 一. 优化oracle中的sql语句,提高运行效率 1. 选择最有效率的表名顺序(只在基于规则的优 ...

  4. Oracle备份恢复之冷备份恢复与异机还原

    (同平台:rhel6.4~rhel6.4:同版本:oracle 11g 11.2.0.1~ oracle 11g 11.2.0.1) 冷备份发生在数据库关闭状态下,直接拷贝物理文件的备份方式,备份数据 ...

  5. GraphLab GraphLab和MapReduce的区别

    https://turi.com/ GraphLab和MapReduce的区别 https://baike.baidu.com/item/GraphLab/16423125 2. GraphLab和M ...

  6. is_link

    'Symbolic Link' to File1  content containing path to File1'Hard Link' to File1 content containing Fi ...

  7. iOS-相关集合类

    第一:NSArrary 1.1:集合的基本方法 1.创建集合   NSArray 是不可变数组,一旦创建完成就不能够对数组进行,添加,删除等操作 NSArray * array = [[NSArray ...

  8. contenttypes - django组件

    一.contenttypes介绍 它的作用:可以通过两个字段让表和N张表创建FK关系 二.ContentType.GenericForeignKey.GenericRelation 表结构: from ...

  9. (3.4)mysql基础深入——mysql.server启动脚本源码阅读与分析

    (3.4)mysql基础深入——mysql.server启动脚本源码阅读与分析 my.server主要分为3大部分 [1]变量初始化部分 [2]函数声明部分 [3]具体执行部分 #!/bin/sh # ...

  10. MySQL Innodb日志机制深入分析

    MySQL Innodb日志机制深入分析 http://blog.csdn.net/yunhua_lee/article/details/6567869 1.1. Log & Checkpoi ...