PHP中CURL实现GET和POST请求
POST方式实现(推荐)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | function postData( $url , $data ){ $ch = curl_init(); curl_setopt( $ch , CURLOPT_URL, $url ); curl_setopt( $ch , CURLOPT_CUSTOMREQUEST, "POST" ); 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); 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 ; } |
GET方式实现直线导轨滑台
1 2 3 4 5 6 7 8 9 10 11 12 | function getData( $url ){ $ch = curl_init(); curl_setopt( $ch , CURLOPT_URL, $url ); curl_setopt( $ch , CURLOPT_HEADER,0); curl_setopt( $ch , CURLOPT_RETURNTRANSFER,1); //禁止调用时就输出获取到的数据 curl_setopt( $ch , CURLOPT_FOLLOWLOCATION,1); curl_setopt( $ch , CURLOPT_SSL_VERIFYPEER,false); curl_setopt( $ch , CURLOPT_SSL_VERIFYHOST,false); $result = curl_exec( $ch ); curl_close( $ch ); return $result ; } |