Example #1
0
 /**
  * 通过curl post数据
  * @param string $url
  * @param array $post_data
  * @param array $header
  * @param string $cookie
  * @param int $timeout
  * @param int $retry_times
  * @param array $curl_status
  * @return mixed
  */
 public static function curlPost($url, $post_data = array(), $header = array(), $cookie = '', $timeout = 5, $retry_times = 0, &$curl_status = array())
 {
     //print_r(array("url"=>$url, "post_data" => $post_data, "header" => $header,"cookie"=>$cookie));
     $post_string = is_array($post_data) ? http_build_query($post_data) : $post_data;
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_POST, true);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
     curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
     curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
     // 模拟的header头
     //设置连接结束后保存cookie信息的文件
     curl_setopt($ch, CURLOPT_COOKIE, $cookie);
     $retry_times < 0 && ($retry_times = 0);
     for ($i = 0; $i <= $retry_times; $i++) {
         if ($i > 0) {
             Lib_Log::info("the request of {$url} time out after {$timeout} sec and retrying the {$i} times and post_data is " . Lib_Array::varExport($post_data));
         }
         $result = curl_exec($ch);
         if ($result !== false) {
             break;
         }
     }
     $curl_status = curl_getinfo($ch);
     curl_close($ch);
     return $result;
 }