PHP发动HTTP申请形式有:一、经过【file_get_contents】发送get申请;二、经过【CURL】发送get申请;三、经过【fsocket】发送get申请。
PHP发动HTTP申请形式有:
curl依然是最佳的HTTP库,不之一。 能够处理任何复杂的使用场景中的HTTP 申请;
文件流式的HTTP申请比拟适宜解决简略的HTTP POST/GET申请,但没有实用于复杂的HTTP申请;
PECL_HTTP扩大写代码愈加简约,省事, 但成熟度欠好,编程接口没有对立,文档以及实例匮乏。
相干学习保举:PHP编程从入门到通晓
一、file_get_contents发送get申请
<?php /** * 发送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' => 'abcdef', 'password' => '123456' ); send_post('http://xxx.com', $post_data);
二、经过CURL发送get申请
<?php $ch=curl_init('http://www.xxx.com/xx.html'); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); curl_setopt($ch,CURLOPT_BINARYTRANSFER,true); $output=curl_exec($ch); $fh=fopen("out.html",'w'); fwrite($fh,$output); fclose($fh);
三、经过fsocket发送get申请
/** * Socket版本 * 应用办法: * $post_string = "app=socket&version=beta"; * request_by_socket('blog.snsgou.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; }
以上就是PHP发动HTTP申请有哪几种形式?的具体内容,更多请存眷资源魔其它相干文章!
标签: php php教程 http请求 php故障解决 php使用问题
版权声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。
抱歉,评论功能暂时关闭!