欢迎各位兄弟 发布技术文章
这里的技术是共享的
最初学习的例子
<?php
// 创建一个cURL资源
$ch = curl_init();
// 设置URL和相应的选项 好像必须要用 www.baidu.com 不要用 baidu.com 否则会跳转
//是由于它本身 网站跳转的吧
curl_setopt($ch, CURLOPT_URL, "http://www.baidu.com/");
curl_setopt($ch, CURLOPT_HEADER, false);
var_dump("QQQQQ");
// 抓取URL并把它传递给浏览器
curl_exec($ch);
var_dump("QQQQQ");
// 关闭cURL资源,并且释放系统资源
curl_close($ch);
?>
下面是史平忠做的王者大陆的例子
<?php
//$rurl = $_GET["url"];
$rurl = 'http://passport.aaaa.com/Service/regService.aspx' ; //需要转到的b.domain.com中的页面
$_GET = array_merge($_GET, $_POST); //处理CGI参数和值
$_GET = array_merge($_POST, $_GET);
//$GET = $_POST;
$qs = "";
$needStrip = get_magic_quotes_gpc();
foreach ($_GET as $k=>$v) {
if ($needStrip) $v = stripslashes($v);
$qs .= "$k=$v&";
}
$ch = curl_init();
$cookie = ""; //处理COOKIE
foreach ($_COOKIE as $key=>$value){
$cookie .= "$key=$value; ";
}
//var_dump($qs);
if ($_SERVER["REQUEST_METHOD"] === "POST") { //POST方法
$options = array(
CURLOPT_URL=>$rurl,
CURLOPT_POST=>true,
CURLOPT_POSTFIELDS=>$qs,
CURLOPT_COOKIE=>$cookie,
);
} else { //GET方法
$options = array(
CURLOPT_URL=>$rurl."?".$qs,
CURLOPT_COOKIE=>$cookie,
);
}
curl_setopt_array($ch, $options);
curl_exec($ch);// 抓取URL并把它传递给浏览器
curl_close($ch);
下面是从网上来的例子
(下面是get方法)
$ch = curl_init("http://www.jb51.net/") ;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ;
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ;
$output = curl_exec($ch) ;
$fh = fopen("out.html", 'w') ;
fwrite($fh, $output) ;
fclose($fh) ;
(下面是post方法)(它这里对参数值进行了url编码)
//extract data from the post
extract($_POST) ;
//set POST variables
$url = 'http://www.jb51.net/get-post.php' ;
$fields = array(
'lname'=>urlencode($last_name) ,
'fname'=>urlencode($first_name) ,
'title'=>urlencode($title) ,
'company'=>urlencode($institution) ,
'age'=>urlencode($age) ,
'email'=>urlencode($email) ,
'phone'=>urlencode($phone)
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&' ; }
rtrim($fields_string ,'&') ;
//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)) ;
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string) ;
//execute post
$result = curl_exec($ch) ;
//close connection
curl_close($ch)
来自 http://www.jb51.net/article/15554.htm
|
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.163.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
?>
当执行curl_exec($ch);后会即刻输出返回内容。
不让它输出而得到它返回的内容的方法。
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
//Tell curl to write the response to a variable 不让它输出而得到它返回的内容的方法。
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
// The maximum number of seconds to allow cURL functions to execute.
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 60);
$buf = curl_exec($c);
将内容赋值给变量
来自 http://bbs.phpchina.com/home.php?mod=space&uid=884691&do=blog&id=193255