<?php
$curl = curl_init();//初始化一个cURL对象
$url = "http://cart.jd.com/cart/cart.html?backurl=http://item.jd.com/176166.html&rid=0.9533184533 938766";
$header = array();
$header[] = ‘User-Agent: 5.0 (iPhone; U; CPU iPhone OS 4_3 like Mac OS X; en-us)‘;
$header[] = ‘Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8‘;
$header[]= ‘Accept-Encoding: gzip,deflate‘;
$header[]= ‘//可以根据需要增加header内容‘;
curl_setopt($curl,CURLOPT_URL, $url);//设置你需要抓去的URL地址
curl_setopt($curl,CURLOPT_HEADER,$header );//设置header
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);//将结果返回输出到字符串
$str = curl_exec($curl);运行cURL,请求网页
curl_close($curl);//关闭url请求
return $str;//返回或者显示结果
?>
如何post数据
//假设我们去post一个表单到http://www.example.com/sendSMS.php
//提交内容 一个是电话号码,一个短信内容
<?php
$phoneNumber = ‘13598785110‘;
$message = ‘this is a test message about cURL to POST URL‘;
$curlPost = ‘pNUMBER=‘.$phoneNumber.‘&MESSAGE=‘.urlencode($message).‘&SUBMIT=Send‘;//拼接请求参数
$url = ‘http://www.example.com/sendSMS.php‘;
$header[] = ‘........‘;
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_HEADER,$header);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl,CURLOPT_POST,1);//设置请求方式为post
curl_seropt($curl,CURLOPT_POSTFIELDS,$curlPost);//设置post数据
$data = curl_exec(curl);
curl_close($curl)
?>
原文地址:http://www.cnblogs.com/tangchuanyang/p/3851039.html