php 异步请求

jerry 2015年11月18日 收藏
/**
* 发送HTTP请求并获得响应
* @param url 请求的url地址
* @param param 发送的http参数
*/
  1. function makeRequest($url, $param, $httpMethod = 'GET') {
  2.         $oCurl = curl_init();
  3.         if (stripos($url, "https://") !== FALSE) {
  4.                 curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
  5.                 curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
  6.         }

  7.         if ($httpMethod == 'GET') {
  8.                 curl_setopt($oCurl, CURLOPT_URL, $url . "?" . http_build_query($param));
  9.                 curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
  10.         } else {
  11.                 curl_setopt($oCurl, CURLOPT_URL, $url);
  12.                 curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
  13.                 curl_setopt($oCurl, CURLOPT_POST, 1);
  14.                 curl_setopt($oCurl, CURLOPT_POSTFIELDS, http_build_query($param));
  15.         }

  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. }