方法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. BC32206 错误

    出现这种情况的话 排除代码引入dll版本的原因 看下是不是 你之前用net reflector 生成了pdb文件 导致项目引入的版本都是他生成的文件 处理方案就是 用everything 找到这个dl ...

  2. 2.1TF模型持久化

    目前tf只能保存模型中的variable变量,整个模型还不能保存,版本1.x 保存模型代码 import tensorflow as tf import numpy as np # Save to f ...

  3. hihocoder 1829 - 压缩字符串 - [状压+暴力枚举][2018ICPC北京网络预赛B题]

    题目链接:https://hihocoder.com/problemset/problem/1829 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Lara Croft, ...

  4. HDU 1754 - I Hate It & UVA 12299 - RMQ with Shifts - [单点/区间修改、区间查询线段树]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 Time Limit: 9000/3000 MS (Java/Others) Memory Li ...

  5. dbAdmin 不等于 root 集合中角色

    dbAdmin  root  集合中角色 use admin创建 db.createUser( { user: "tmp_rw_56756", pwd: "tmp4242 ...

  6. UITableView _endCellAnimationsWithContext崩溃

    http://www.cocoachina.com/bbs/read.php?tid-315222.html 删除cell之前先把数据源也删除一条,直接删除cell会崩溃 下面是正确的姿势: cell ...

  7. 涉及不同实例不同数据库的同一条sql语句

    exec sp_configure 'show advanced options',1 reconfigure exec sp_configure 'Ad Hoc Distributed Querie ...

  8. Reporting Service Url传递参数应用

    与sharepoint集成的报表传递参数示例: http://reportserver/reportcenter/_layouts/ReportServer/RSViewerPage.aspx?rv: ...

  9. go-001-环境部署,IDEA插件

    一.下载安装 https://golang.org/dl/ 下载之后安装即可 官网地址:https://golang.org/ 1.1.mac上安装go 1.安装Homebrew 安装命令: ruby ...

  10. awesome go library 库,推荐使用的golang库

    https://github.com/avelino/awesome-go https://github.com/spf13/cobra                        # A Comm ...