2010-06-21

HTTP POST using PHP cURL

Views: 21607 | Add Comments
function http_post($url, $data){
	if(is_array($data)){
		$data = http_build_query($data);
	}
	$ch = curl_init($url) ; 
	curl_setopt($ch, CURLOPT_POST, 1) ;
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1) ;
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	$result = curl_exec($ch) ; 
	curl_close($ch) ;
	return $result;
}

function http_get($url){
	$ch = curl_init($url) ; 
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1) ;
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	$result = curl_exec($ch) ; 
	curl_close($ch) ;
	return $result;
}

获取 HTTP 请求的首部:

curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$result = curl_exec($ch) ;
echo curl_getinfo($ch, CURLINFO_HEADER_OUT);

cURL 打印请求头和响应头的选项 CURLOPT_VERBOSE

HTTPS:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

Related posts:

  1. PHP 用 curl 读取 HTTP chunked 数据
  2. PHP重用curl句柄, CURLOPT_HTTPGET的BUG
  3. 史上最强大的PHP MySQL操作类
  4. iOS 正确接收 HTTP chunked 数据的方法
  5. 通过 HTTP POST 发送二进制数据
Posted by ideawu at 2010-06-21 14:44:40 Tags:

Leave a Comment