方法一:
/**
* 发送post请求
* @param string $url 请求地址
* @param array $post_data post键值对数据
* @return string
*/
function send_post($url, $post_data) {
$postdata = http_build_query($post_data);
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => $postdata,
'timeout' => 15 * 60 // 超时时间(单位:s)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
//使用方法
$post_data = array(
'username' => 'stclair2201',
'password' => 'handan'
);
send_post('//www.gxlcms.com', $post_data);
方法二:Socket版本
/**
* Socket版本
* 使用方法:
* $post_string = "app=socket&version=beta";
* request_by_socket('chajia8.com', '/restServer.php', $post_string);
*/
function request_by_socket($remote_server,$remote_path,$post_string,$port = 80,$timeout = 30) {
$socket = fsockopen($remote_server, $port, $errno, $errstr, $timeout);
if (!$socket) die("$errstr($errno)");
fwrite($socket, "POST $remote_path HTTP/1.0");
fwrite($socket, "User-Agent: Socket Example");
fwrite($socket, "HOST: $remote_server");
fwrite($socket, "Content-type: application/x-www-form-urlencoded");
fwrite($socket, "Content-length: " . (strlen($post_string) + 8) . "");
fwrite($socket, "Accept:*/*");
fwrite($socket, "");
fwrite($socket, "mypost=$post_string");
fwrite($socket, "");
$header = "";
while ($str = trim(fgets($socket, 4096))) {
$header .= $str;
}
$data = "";
while (!feof($socket)) {
$data .= fgets($socket, 4096);
}
return $data;
}
?>
方法三:Curl版本
/**
* Curl版本
* 使用方法:
* $post_string = "app=request&version=beta";
* request_by_curl('//www.gxlcms.com/restServer.php', $post_string);
*/
function request_by_curl($remote_server, $post_string) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $remote_server);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'mypost=' . $post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "jb51.net's CURL Example beta");
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
下面是其他网友的方法:
class Request{
public static function post($url, $post_data = '', $timeout = 5){//curl
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
if($post_data != ''){
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HEADER, false);
$file_contents = curl_exec($ch);
curl_close($ch);
return $file_contents;
}
public static function post2($url, $data){//file_get_content
$postdata = http_build_query(
$data
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
return $result;
}
public static function post3($host,$path,$query,$others=''){//fsocket
$post="POST $path HTTP/1.1\r\nHost: $host\r\n";
$post.="Content-type: application/x-www-form-";
$post.="urlencoded\r\n${others}";
$post.="User-Agent: Mozilla 4.0\r\nContent-length: ";
$post.=strlen($query)."\r\nConnection: close\r\n\r\n$query";
$h=fsockopen($host,80);
fwrite($h,$post);
for($a=0,$r='';!$a;){
$b=fread($h,8192);
$r.=$b;
$a=(($b=='')?1:0);
}
fclose($h);
return $r;
}
}
https://blog.****.net/weixin_42500195/article/details/115061695?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-4.control&spm=1001.2101.3001.4242
大家可以根据需要选择适合自己的即可。
本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉
本文系统来源:php中文网
TAG标签:脚本
————————————————
版权声明:本文为****博主「智能嘿嘿」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.****.net/weixin_42500195/article/details/115061695
php 重定向与转发
https://blog.****.net/qq_29381417/article/details/80950697?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-2.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-2.control
1、重定向
1)header重定向
header('location:https://www.baidu.com');
//确保后续代码不执行
die();
2)使用meta标签
<?php
< html>
< head>
< meta http-equiv="refresh" content="1;
url=< ?php echo 'www.baidu.com'; ?>">
< /head>
< body>
页面只停留一秒……
< /body>
< /html>
3)javascript
< ?php
echo "< script language='javascript'
type='text/javascript'>";
echo "window.location.href='http://www.baidu.com'";
echo "< /script>";
?>
2、转发
1)curl转发
<?php
function get_url_content($url){
$user_agent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)";
$ch = curl_init();
//curl_setopt ($ch, CURLOPT_PROXY, $proxy);
curl_setopt ($ch, CURLOPT_URL, $url);//设置要访问的IP
curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);//模拟用户使用的浏览器
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1 ); // 使用自动跳转
curl_setopt ($ch, CURLOPT_TIMEOUT, 60); //设置超时时间
curl_setopt ($ch, CURLOPT_AUTOREFERER, 1 ); // 自动设置Referer
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'c:\cookie.txt');
curl_setopt ($ch, CURLOPT_HEADER,0); //显示返回的HEAD区域的内容
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_TIMEOUT, 10);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
print_r(get_url_content('http://www.baidu.com'));
?>
2)file_get_contents()转发
print_r(file_get_contents('http://www.baidu.com'));
3、总结
1)转发作用于服务器端,是在服务器内部进行转发。重定向作用于客户端,相当于客户端重新发送一次新的请求。
2)转发后地址栏不会改变。重定向后地址栏改变。
3)转发后资源能获取到请求中的数据。重定向后的资源不能获得原请求中的数据。
4)转发只能在本应用内部资源之间进行。重定向可以跳转到任何网络资源。
5)转发可以访问受保护资源(WEB-INF里的资源)。重定向不能定位到受保护资源。
————————————————
版权声明:本文为****博主「yuehua_520」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.****.net/qq_29381417/article/details/80950697