GET方式
$ch = curl_init();//初始化curl
curl_setopt($ch,CURLOPT_URL,‘网址’);//抓取指定网页
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 获取数据返回
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回
echo $output = curl_exec($ch) ;
POST方式
$url = 'http://www.domain.com/api/' ;
$fields = array(
'name'=>'sam' ,
'title'=>'myphp',
'age'=>'25' ,
'email'=>'cbsahhs@163.com' ,
'site'=>'http://www.qianshou13.org'
);
//$post_data = implode('&',$fields);
//open connection
$ch = curl_init() ;
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL,$url) ;
curl_setopt($ch, CURLOPT_POST,count($fields)) ; // 启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields); // 在HTTP中的“POST”操作。如果要传送一个文件,需要一个@开头的文件名
ob_start();
curl_exec($ch);
$result = ob_get_contents() ;
ob_end_clean();
echo $result;
//close connection
curl_close($ch) ;
cURL 是一种功能强大的库,支持很多不同的协议、选项,能提供 URL 请求相关的各种细节信息。处理coockies、验证、表单提交、文件上传等等。