php发送get、post请求的6种方法简明总结

作者:junjie 时间:2023-11-14 12:59:43 

方法1: 用file_get_contents 以get方式获取内容:

<?php
$url='https://www.aspxhome.com/';
$html = file_get_contents($url);
echo $html;
?>

方法2: 用fopen打开url, 以get方式获取内容:

<?php
$fp = fopen($url, ‘r');
stream_get_meta_data($fp);
while(!feof($fp)) {
$result .= fgets($fp, 1024);
}
echo “url body: $result”;
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) . “rn”,
‘content' => $data
)
);
$context = stream_context_create($opts);
$html = file_get_contents(‘http://localhost/e/admin/test.html', 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:”.$query;
$fp = fsockopen( $url[host], $url[port]?$url[port]:80 , $errno, $errstr, 30);
if (!$fp) {
return false;
} else {
$request = “GET $query HTTP/1.1rn”;
$request .= “Host: $url[host]rn”;
$request .= “Connection: Closern”;
if($cookie) $request.=”Cookie:  $cookien”;
$request.=”rn”;
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.1n”;
$request.=”Host: “.$URL_Info["host"].”n”;
$request.=”Referer: $referern”;
$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, ‘https://www.aspxhome.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,get,post
0
投稿

猜你喜欢

  • 7个Python中的隐藏小技巧分享

    2022-06-20 22:36:14
  • 浅谈validator自定义验证及易错点

    2024-05-02 16:58:41
  • MySQL详解如何优化查询条件

    2024-01-26 06:52:05
  • Python + opencv对拍照得到的图片进行背景去除的实现方法

    2022-09-06 19:14:51
  • SpringBoot配置数据库密码加密的实现

    2024-01-26 07:33:01
  • python字符串string的内置方法实例详解

    2022-06-14 01:10:23
  • python删除列表中特定元素的几种方法

    2023-12-21 02:17:12
  • Go编译原理之函数内联

    2024-05-22 10:12:38
  • django在开发中取消外键约束的实现

    2021-10-12 05:47:57
  • 如何使用ADO.NET连接数据库?

    2010-06-03 10:52:00
  • Mootools 1.2教程(18)——Class 类(第一部分)

    2008-12-19 12:45:00
  • c#数据库与TXT导入导出的实例

    2024-01-24 06:34:16
  • mysql实现设置定时任务的方法分析

    2024-01-18 03:37:18
  • 深入SQL Server中定长char(n)与变长varchar(n)的区别详解

    2024-01-14 01:53:42
  • tornado捕获和处理404错误的方法

    2023-11-27 11:03:36
  • 微软的jQuery国际化插件

    2010-07-02 12:46:00
  • python异常处理之try finally不报错的原因

    2023-05-01 00:02:40
  • python中shell执行知识点

    2022-11-30 08:41:15
  • python中join与os.path.join()函数实例详解

    2023-08-23 19:20:51
  • 基于Python os模块常用命令介绍

    2023-09-09 01:53:43
  • asp之家 网络编程 m.aspxhome.com