获取http内容的php函数

本文介绍了一个用PHP实现的HTTP请求函数,该函数可以发送GET或POST请求,并支持设置超时时间、User-Agent、Cookie等参数。通过使用stream_context_create和file_get_contents函数,实现了灵活的HTTP请求方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现获取http内容的php函数。

代码如下:

<?php
function http_open($url, $data, $cookie = null, $method = "GET", $timeout = 60) {
$options = array();
$options['http']['method'] = $method;
$options['http']['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
$options['http']['timeout'] = $timeout;
if($method == "POST") : 
$length = strlen($data);
//  www.jbxue.com
$options['http']['header'] = "Content-type: application/x-www-form-urlencoded\r\n".
"Content-Length: {$length}\r\n".
"P3P: CP=\"CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR\"\r\n".
"cookie: {$cookie}\r\n".
"Connection: close\r\n";
$options['http']['content'] = $data;
else:
$options['http']['header'] = "Content-type: application/x-www-form-urlencoded\r\n".
"P3P: CP=\"CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR\"\r\n".
"cookie: {$cookie}\r\n".
"Connection: close\r\n";
endif;

$context = stream_context_create($options);
return file_get_contents($url, 0, $context);
}
echo http_open("http://www.jbxue.com/1.php", "username=haowei", "id=5", "POST");

转载于:https://www.cnblogs.com/cfinder010/p/3552080.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值