记住用户名密码
在开发的时候,需要请求别人的接口,那么就要用到curl了
由于很多地方都会用到,就封装了两个,以后应该都会用到
/* * @desc curl POST 方式请求接口 */ function post_api($path, $post){ $curl = curl_init(); $header[] = "Content-type: text/html;charset=utf-8"; curl_setopt($curl, CURLOPT_URL, "$path"); // curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");//3.请求方式 curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10 QQDownload/1.7'); //curl_setopt($curl, CURLOPT_REFERER, "xxx"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_HTTPHEADER, $header); //curl_setopt($curl, CURLOPT_POST, 80); curl_setopt($curl, CURLOPT_POSTFIELDS, $post); // curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:')); $contents = curl_exec($curl); // var_dump($contents); curl_close($curl); return $contents; } /** * get 请求的curl */ function get_api($url){ $curl = curl_init(); // 启动一个CURL会话 $header[] = "Content-type: text/html;charset=utf-8"; curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_HTTPHEADER, $header); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在 curl_setopt($curl,CURLOPT_ENCODING,'gzip,deflate'); $contents = curl_exec($curl); //返回api的json对象 $contents = mb_convert_encoding($contents, 'utf-8', 'GBK,UTF-8,ASCII'); //关闭URL请求 curl_close($curl); // dd($contents); return $contents; //返回json对象 // $ch = curl_init(); // curl_setopt($ch, CURLOPT_URL, $url); // curl_setopt($ch, CURLOPT_HEADER, false); // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //// curl_setopt($ch, CURLOPT_ENCODING, ""); // curl_setopt($ch,CURLOPT_ENCODING,'gzip,deflate'); // curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.1 Safari/537.11'); // $res = curl_exec($ch); // $rescode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // curl_close($ch) ; // return $res; // $curl = curl_init(); // $header[] = "Content-type: text/html;charset=utf-8"; // curl_setopt($curl, CURLOPT_URL, "$url"); //// curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");//3.请求方式 // curl_setopt($curl, CURLOPT_USERAGENT, // 'Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10 QQDownload/1.7'); // //curl_setopt($curl, CURLOPT_REFERER, "xxx"); // curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // curl_setopt($curl, CURLOPT_HTTPHEADER, $header); // //curl_setopt($curl, CURLOPT_POST, 80); //// curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:')); // $contents = curl_exec($curl); // $contents = mb_convert_encoding($contents, 'utf-8', 'GBK,UTF-8,ASCII'); //// var_dump($contents); // curl_close($curl); // return $contents; }
目前有 0 条留言 其中:访客:0 条, 博主:0 条