Curl请求显示与浏览器请求不同的内容


Curl request showing different content from that of browser request

当我在浏览器中键入URL时,它会返回详细的输出。然而,当我尝试通过curl请求来实现这一点时,请求是空的。为什么会发生这种情况?

URL为https://api.500px.com/v1/users?oauth_token=AihBz6ZWedu3VxnQdy2tqWtbwV86wtOuXumhPapk&oauth_ verifier=YhKo0kaGhfw0dFhparxU&consumer_key=0OvWThqr5j1ZYX1cPaa8y0y1aOfJBbDtpX85fJ42

我的代码是:

<!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>

通过cURL和在浏览器中手动发出请求可以显示不同的内容,即使您在这两种情况下都提交了相同的URL参数。原因可能很少。

  • POST数据:您的浏览器正在发送一些POST数据,在cURL的情况下您忘记发送这些数据。

  • Cookie:您的网络浏览器正在向服务器发送一些显示内容所需的Cookie。

  • IP:在本地服务器的情况下这不是一个问题,但在网络主机的情况下可能会有所不同。