CURL 模拟http提交
Posted penven
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CURL 模拟http提交相关的知识,希望对你有一定的参考价值。
1:CURL模拟get提交
private function httpGet($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 500); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_URL, $url); $res = curl_exec($curl); curl_close($curl); return $res; }
2:CURL模拟post提交
//模拟post提交 function http_request($url , $data , $method="POST"){ $ch = curl_init(); //初始化 curl_setopt($ch , CURLOPT_URL , $url); //请求地址 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //请求方式 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_USERAGENT, ‘Mozilla/5.0 (compatible; MSIE 5.01; windows NT 5.0)‘); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_AUTOREFERER, 1); if($method == ‘POST‘){ //POST方式的时候添加数据 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $tmpinfo = curl_exec($ch); //执行 if(curl_errno($ch)){ return curl_error($ch); } curl_close($ch); //关闭 return $tmpinfo; } curl模拟post提交 function https_request($url, $data = null){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)){ curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); return $output; }
以上是关于CURL 模拟http提交的主要内容,如果未能解决你的问题,请参考以下文章