您好,欢迎访问一九零五行业门户网

php中Curl的请求方式有哪些?php curl的四种请求方式介绍

本篇文章给大家带来的内容是关于php中curl的请求方式有哪些?php curl的四种请求方式介绍,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
1、发送json格式数据,请求地址:https
protected 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); //下面这行是修改后增加的代码,就是配置设置host访问,发送的数据类型为application/json curl_setopt($curl, curlopt_httpheader, array( 'content-type: application/json; charset=utf-8', 'content-length: ' . strlen($data) )); $output = curl_exec($curl); curl_close($curl); return $output;}
2、发送json格式数据,请求地址:http
protected function curlpost($url, $data){ $ch = curl_init($url); curl_setopt($ch, curlopt_customrequest, "post"); curl_setopt($ch, curlopt_postfields, $data);//$data json类型字符串 curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch, curlopt_httpheader, array('content-type: application/json', 'content-length: ' . strlen($data))); $result = curl_exec($ch); curl_close ( $ch ); return $result;}
3、表单格式提交
function file_get_contents_post($url, $post){ $options = array( 'http'=> array( 'method'=>'post', 'header' => "content-type: application/x-www-form-urlencoded ", 'content'=> http_build_query($post), ), ); $result = file_get_contents($url,false, stream_context_create($options)); return $result;}$datare = file_get_contents_post("http://103.72.165.183/api/payment.aspx", $data);var_dump($datare);
4、$url是地址加数据的形式:http://baidu.com?a="ss"&b="ds";
public function getsslhttp($url){ $curl = curl_init(); curl_setopt($curl, curlopt_url, $url); curl_setopt($curl, curlopt_header, false); curl_setopt($curl, curlopt_returntransfer, true); curl_setopt($curl, curlopt_ssl_verifypeer, false); // https请求 不验证证书和hosts curl_setopt($curl, curlopt_ssl_verifyhost, false); $data = curl_exec($curl); $httpcode = curl_getinfo($curl,curlinfo_http_code); if ( $httpcode != 200 ){ $data="https connect timeout"; } curl_close($curl); return $data; }
相关推荐:
php之curl实现http与https请求的方法,phpcurlhttps请求
php发送post请求的三种方式
以上就是php中curl的请求方式有哪些?php curl的四种请求方式介绍的详细内容。
其它类似信息

推荐信息