1 /** 2 * GET 请求 3 * 4 * @param string $url 5 */ 6 private function http_get($url) 7 { 8 $oCurl = curl_init(); 9 if (stripos($url, "https://") !== FALSE) { 10 curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE); 11 curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE); 12 curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); // CURL_SSLVERSION_TLSv1 13 } 14 curl_setopt($oCurl, CURLOPT_URL, $url); 15 curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1); 16 $sContent = curl_exec($oCurl); 17 $aStatus = curl_getinfo($oCurl); 18 curl_close($oCurl); 19 if (intval($aStatus["http_code"]) == 200) { 20 return $sContent; 21 } else { 22 return false; 23 } 24 } 25 26 /** 27 * POST 请求 28 * 29 * @param string $url 30 * @param array $param 31 * @param boolean $post_file 32 * 是否文件上传 33 * @return string content 34 */ 35 private function http_post($url, $param, $post_file = false) 36 { 37 $oCurl = curl_init(); 38 if (stripos($url, "https://") !== FALSE) { 39 curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE); 40 curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false); 41 curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); // CURL_SSLVERSION_TLSv1 42 } 43 if (PHP_VERSION_ID >= 50500 && class_exists('CURLFile')) { 44 $is_curlFile = true; 45 } else { 46 $is_curlFile = false; 47 if (defined('CURLOPT_SAFE_UPLOAD')) { 48 curl_setopt($oCurl, CURLOPT_SAFE_UPLOAD, false); 49 } 50 } 51 if (is_string($param)) { 52 $strPOST = $param; 53 } elseif ($post_file) { 54 if ($is_curlFile) { 55 foreach ($param as $key => $val) { 56 if (substr($val, 0, 1) == '@') { 57 $param[$key] = new CURLFile(realpath(substr($val, 1))); 58 } 59 } 60 } 61 $strPOST = $param; 62 } else { 63 $aPOST = array(); 64 foreach ($param as $key => $val) { 65 $aPOST[] = $key . "=" . urlencode($val); 66 } 67 $strPOST = join("&", $aPOST); 68 } 69 curl_setopt($oCurl, CURLOPT_URL, $url); 70 curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1); 71 curl_setopt($oCurl, CURLOPT_POST, true); 72 curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST); 73 $sContent = curl_exec($oCurl); 74 $aStatus = curl_getinfo($oCurl); 75 curl_close($oCurl); 76 if (intval($aStatus["http_code"]) == 200) { 77 return $sContent; 78 } else { 79 return false; 80 } 81 } 82 83 84 ############################################################################ 85 86 87 public function send_post( $url, $post_data ) { 88 $options = array( 89 'http' => array( 90 'method' => 'POST', 91 'header' => 'Content-type:application/json', 92 //header 需要设置为 JSON 93 'content' => $post_data, 94 'timeout' => 60 95 //超时时间 96 ) 97 ); 98 99 $context = stream_context_create( $options ); 100 $result = file_get_contents( $url, false, $context ); 101 102 return $result; 103 }