php curl post提交
/** * @param $arr //提交的数据 * @param $url //请求地址 * @param $type //当type == 1时为表单提交 * @return mixed */ public function postCurl($arr,$url,$type){ $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,$url);//获取url地址 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch,CURLOPT_HEADER,0);//启用时会将头文件的信息作为数据流输出 curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//TRUE 将curl_exec()获取的信息以字符串返回,而不是直接输出 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100);//设置连接等待时间 curl_setopt($ch, CURLOPT_TIMEOUT, 10);//超时时间 curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 ); //强制使用IPV4协议解析域名 curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); //强制协议为1.0 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//忽略证书 if($type == 1){ curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($arr)); }else{ $json = json_encode($arr); curl_setopt($ch, CURLOPT_POSTFIELDS,$json); curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Content-Type: application/json;charset='utf-8'", 'Content-Length: ' . strlen($json) )); } $res = curl_exec($ch); if (curl_errno($ch)) { echo curl_error($ch);exit;//捕抓异常 } curl_close($ch); $result = json_decode($res,true); return $result; }
本文来自博客园,作者:小ྀ青ྀ年້,转载请注明原文链接:https://www.cnblogs.com/dalaowang/p/13186087.html