php转发post_php发送post请求的三种方法

方法一:

/**

* 发送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 Host: $host ";

$post.="Content-type: application/x-www-form-";

$post.="urlencoded ${others}";

$post.="User-Agent: Mozilla 4.0 Content-length: ";

$post.=strlen($query)." Connection: close $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.csdn.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标签:脚本
————————————————
版权声明:本文为CSDN博主「智能嘿嘿」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_42500195/article/details/115061695

php 重定向与转发

https://blog.csdn.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里的资源)。重定向不能定位到受保护资源。
————————————————
版权声明:本文为CSDN博主「yuehua_520」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_29381417/article/details/80950697

php导航异步请求,php异步请求的四种实现方式

https://blog.csdn.net/weixin_42373088/article/details/115165796?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control

本文为大家讲述了php异步调用方法,分享给大家供大家参考,具体内容如下

客户端与服务器端是通过HTTP协议进行连接通讯,客户端发起请求,服务器端接收到请求后执行处理,并返回处理结果。

有时服务器需要执行很耗时的操作,这个操作的结果并不需要返回给客户端。但因为php是同步执行的,所以客户端需要等待服务处理完才可以进行下一步。

因此对于耗时的操作适合异步执行,服务器接收到请求后,处理完客户端需要的数据就返回,再异步在服务器执行耗时的操作。

1.使用Ajax 与 img 标记

原理,服务器返回的html中插入Ajax 代码或 img 标记,img的src为需要执行的程序。

优点:实现简单,服务端无需执行任何调用

缺点:在执行期间,浏览器会一直处于loading状态,因此这种方法并不算真正的异步调用。$.get("doRequest.php", { name: "fdipzone"} );

2.使用popen

使用popen执行命令,语法:// popen — 打开进程文件指针

resource popen ( string $command , string $mode )

pclose(popen('php /home/fdipzone/doRequest.php &', 'r'));

优点:执行速度快

缺点:

1).只能在本机执行

2).不能传递大量参数

3).访问量高时会创建很多进程

3.使用curl

设置curl的超时时间 CURLOPT_TIMEOUT 为1 (最小为1),因此客户端需要等待1秒<?php

$ch = curl_init();

$curl_opt = array(

CURLOPT_URL, 'http://www.example.com/doRequest.php'

CURLOPT_RETURNTRANSFER,1,

CURLOPT_TIMEOUT,1

);

curl_setopt_array($ch, $curl_opt);

curl_exec($ch);

curl_close($ch);

?>

4.使用fsockopen

fsockopen是最好的,缺点是需要自己拼接header部分。<?php

$url = 'http://www.example.com/doRequest.php';

$param = array(

'name'=>'fdipzone',

'gender'=>'male',

'age'=>30

);

doRequest($url, $param);

function doRequest($url, $param=array()){

$urlinfo = parse_url($url);

$host = $urlinfo['host'];

$path = $urlinfo['path'];

$query = isset($param)? http_build_query($param) : '';

$port = 80;

$errno = 0;

$errstr = '';

$timeout = 10;

$fp = fsockopen($host, $port, $errno, $errstr, $timeout);

$out = "POST ".$path." HTTP/1.1 ";

$out .= "host:".$host." ";

$out .= "content-length:".strlen($query)." ";

$out .= "content-type:application/x-www-form-urlencoded ";

$out .= "connection:close ";

$out .= $query;

fputs($fp, $out);

fclose($fp);

}

?>

注意:当执行过程中,客户端连接断开或连接超时,都会有可能造成执行不完整,因此需要加上ignore_user_abort(true); // 忽略客户端断开

set_time_limit(0);    // 设置执行不超时

以上就是php异步调用方法的详细介绍,希望对大家的学习有所帮助。

https://blog.csdn.net/u013179809/article/details/48712893

https://blog.csdn.net/weixin_34185396/article/details/116440324?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_utm_term~default-0.control&spm=1001.2101.3001.4242

https://blog.csdn.net/weixin_29284657/article/details/115108409?utm_term=php%E5%AE%9E%E7%8E%B0%E6%95%B0%E6%8D%AE%E8%BD%AC%E5%8F%91&utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~sobaiduweb~default-6-115108409&spm=3001.4430

https://blog.csdn.net/weixin_31002059/article/details/115938489?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_utm_term~default-4.control&spm=1001.2101.3001.4242

https://blog.csdn.net/habout632/article/details/7882045

https://blog.csdn.net/weixin_33733742/article/details/115061692?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-8.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-8.control

https://blog.csdn.net/cuiweijie3/article/details/10113977

原文地址:https://www.cnblogs.com/yipianchuyun/p/15121418.html