问题描述
那么如何使用 codeigniter 检查客户端是否为 curl,然后为其返回不同的内容?
你可以在使用 cURL 时伪造用户代理,所以当你知道它是一个卷曲请求.
例如:我最近编写了一个应用程序,它可以从谷歌获取一个 url 的 pagerank.现在谷歌不喜欢这个,所以它只允许某个用户代理访问它的pagerank服务器.解决方案?使用 cURL 欺骗用户代理,Google 也不会更聪明.
故事的寓意:cURL 用户代理并不可靠.
如果你还想这样做,那么你应该能够像往常一样获得通过的用户代理
$userAgent=$_SERVER['HTTP_USER_AGENT'];
编辑一个快速测试证明了这一点:
dumpx.php:
dump.php:
案例 1:http://localhost/dumpx.php?u=y
'HTTP_USER_AGENT' =>字符串嘘!"(长度=7)
案例 2:http://localhost/dumpx.php?u=n
没有 $_SERVER['HTTP_USER_AGENT']
这证明 curl 没有默认的用户代理:它只是不会在请求头中传递它
So how can I check using codeigniter if the client is curl, and then return something different for it?
You can fake the user-agent when using cURL, so it's pointless depending on the user-agent sent when you KNOW it's a cURL request.
For example: I recently wrote an app which gets the pagerank of a url from google. Now Google doesn't like this, so it allows only a certain user agent to access its pagerank servers. Solution? Spoof the user-agent using cURL and Google will be none the wiser.
Moral of the story: cURL user agents are JUST NOT reliable.
If you still want to do this, then you should be able to get the passed user agent just like normal
$userAgent=$_SERVER['HTTP_USER_AGENT'];
EDIT A quick test proved this:
dumpx.php:
<?php
$url="http://localhost/dump.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
if($_GET['u']==y) {
curl_setopt($ch, CURLOPT_USERAGENT, "booyah!");
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
//curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'GET');
curl_setopt ($ch, CURLOPT_HEADER, 0);
$exec=curl_exec ($ch);
?>
dump.php:
<?php
var_dump($_SERVER);
?>
Case 1: http://localhost/dumpx.php?u=y
'HTTP_USER_AGENT' => string 'booyah!' (length=7)
Case 2: http://localhost/dumpx.php?u=n
No $_SERVER['HTTP_USER_AGENT']
This proves that there is no default user agent for curl: it will just not pass it in the request header
这篇关于CURL 用户代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2