问题描述
那么,如果客户端是curl,那么如何检查使用codeigniter,然后返回不同的东西呢?
可以假使用cURL时的用户代理,所以它是无意义的,这取决于用户代理发送时,当你知道它是一个cURL请求。
例如:我最近写了一个应用程序,它从google获取一个url的pagerank。现在Google不喜欢这样,所以它只允许某个用户代理访问其pagerank服务器。解?
故事的道德:cURL用户代理是绝对不可靠的。
如果你仍然想要这样做,你应该能像正常一样获得传递的用户代理
$ userAgent = $ _ SERVER ['HTTP_USER_AGENT'];
EDIT 快速测试证明了这一点:
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);
?>
案例1:
'HTTP_USER_AGENT '=>案例2:
否$ _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用户代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!