Skip to content

Instantly share code, notes, and snippets.

@LegendZhu
Created September 12, 2013 16:35
Show Gist options
  • Save LegendZhu/6540457 to your computer and use it in GitHub Desktop.
Save LegendZhu/6540457 to your computer and use it in GitHub Desktop.
一个基本的curl请求
<?php
protected $timeout = 10;
protected $connect_timeout = 2;
private $return_transfer = TRUE;
private $post = TRUE;
/**
* 构造CURL请求
* @param $url
* @param $posts
*/
protected function request($url, $posts) {
$ch = curl_init ();
$options = array (
CURLOPT_URL => $url,
// CURLOPT_DNS_USE_GLOBAL_CACHE => false,
CURLOPT_CONNECTTIMEOUT => $this->connect_timeout,
CURLOPT_TIMEOUT => $this->timeout,
CURLOPT_RETURNTRANSFER => $this->return_transfer,
CURLOPT_POST => $this->post,
CURLOPT_POSTFIELDS => $posts,
CURLOPT_USERAGENT => $_SERVER['HTTP_USER_AGENT']//'Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0'
);
curl_setopt_array ( $ch, $options );
$retval = curl_exec ( $ch );
if ($retval === FALSE) {
$retval = "cURL Error: " . curl_error($ch);
}
curl_close($ch);
return $retval;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment