实现获取http内容的php函数分享
程序员文章站
2023-11-26 14:40:58
复制代码 代码如下:
<?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);
$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://localhost/1.php", "username=haowei", "id=5", "post");
复制代码 代码如下:
<?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);
$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://localhost/1.php", "username=haowei", "id=5", "post");
下一篇: JQUERY dialog的用法详细解析