PHP多种方式发送POST请求
PHP #post2014-02-17 21:12
下面列出3种发送POST请求的方法。
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; } 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; } 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 yige.org\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; }
相关文章
- PHP根据日期判断星座的函数 2013/03/22
- PHP显示搜索引擎来的关键词的函数 2013/03/21
- PHP记录用户通过搜索引擎进网站的关键词代码 2013/03/21
- 错误 Class 'SoapClient' not found in PHP 解决方法 2013/02/27
- PHP根据ISBN获取图书的方法 2013/01/14
- smarty中时间格式化函数date_format 2012/12/28
- PHP防注入安全代码 2012/12/21
- eval()一个有意思的PHP函数 2012/12/21
- php的免杀小马 2012/12/21
- PHP中eval()的小技巧 2012/12/21