Curl请求显示与浏览器请求不同的内容 [英] Curl request showing different content from that of browser request
问题描述
当我在浏览器中输入网址时,会返回详细的输出。但是,当我尝试通过curl请求这样做时,请求是空的。为什么会这样?
我的代码是:
<!DOCTYPE html&
< PHP
功能fetchData($网址){
$ CH = curl_init();
curl_setopt($ ch,CURLOPT_HEADER,1);
curl_setopt($ ch,CURLOPT_VERBOSE,1);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ ch,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ ch,CURLOPT_FAILONERROR,0);
curl_setopt($ ch,CURLOPT_URL,$ url);
$ returned = curl_exec($ ch);
echo'Errors:'。 curl_errno($ ch)。 ''。 curl_error($ ch)。 '< br>< br>';
curl_close($ ch);
echo $ returned;
return $ returned;
}
);
//拉取和解析数据。
$ =返回fetchData(https://api.500px.com/v1/users?oauth_token=xElRwQ6cqItG8Siy9kFBpwkj5sCdlp33NRva5TZU&oauth_verifier=hbNdYnqm8BSyuiZYa4KZ&consumer_key=0OvWThqr5j1ZYX1cPaa8y0y1aOfJBbDtpX85fJ42);
var_dump($ returned);
if(curl_exec($ ch)=== false)
{
echo'Curl error:'。 curl_error($ ch);
}
else
{
echo'操作完成没有任何错误';
}
$ result = json_decode($ returned);
print_r($ returned);
//如果
// {
$ //信息= curl_getinfo($ CH)(curl_errno($ CH)!);
// echo'Took'。 $ info ['total_time']。 '发送请求的秒数'。 $ info ['url'];
//}
echoworking;
echo curl_error($ ch);
echoworkign4;
print_r(curl_getinfo($ ch));
echoworking6;
?>
< / html>
经过卷曲,并在浏览器中手动制作要求可以显示不同的即使您在这两种情况下都提交了相同的网址参数。原因可能很少。
-
POST数据:您的浏览器发送的一些POST数据,如果是cURL
-
Cookie:您的网络浏览器正在向服务器发送一些Cookie以显示内容。
-
IP:对于本地服务器,这不是一个麻烦,但在网络主机的情况下可能会有所不同。
When I type a URL into my browser, it returns detailed output. However, when I try to do this by a curl request, the request is empty. Why this is happening?
My code is:
<!DOCTYPE html>
<?php
function fetchData($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FAILONERROR, 0);
curl_setopt($ch, CURLOPT_URL, $url);
$returned = curl_exec($ch);
echo 'Errors: ' . curl_errno($ch) . ' ' . curl_error($ch) . '<br><br>';
curl_close ($ch);
echo $returned;
return $returned;
}
);
// Pulls and parses data.
$returned = fetchData("https://api.500px.com/v1/users?oauth_token=xElRwQ6cqItG8Siy9kFBpwkj5sCdlp33NRva5TZU&oauth_verifier=hbNdYnqm8BSyuiZYa4KZ&consumer_key=0OvWThqr5j1ZYX1cPaa8y0y1aOfJBbDtpX85fJ42");
var_dump($returned);
if(curl_exec($ch) === false)
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
echo 'Operation completed without any errors';
}
$result = json_decode($returned);
print_r($returned);
// if(!curl_errno($ch))
// {
// $info = curl_getinfo($ch);
// echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];
// }
echo "working";
echo curl_error($ch);
echo "workign4";
print_r(curl_getinfo($ch));
echo "working6";
?>
</html>
Making request through cURL and manually in browser can display different contents even if you have submitted the same URL parameters in both cases. The reasons can be few.
POST Data: Your browser is sending some POST data which you forgot to send in case of cURL.
Cookie: Your web browser is sending some cookie to the server which is required for the content to display.
IP: It is not a trouble in case of local server but can vary in case of a web host.
这篇关于Curl请求显示与浏览器请求不同的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!