cURL 不返回任何内容

发布于 2024-12-28 00:32:48 字数 2175 浏览 2 评论 0原文

cURL 在服务器上时不返回任何内容。一切在本地主机上都运行良好,但是当它在远程托管中时, getSearchResults() 不会返回任何内容(或 302 标头)。服务器配置是否有问题(尝试了 2 个不同的)。可以是 CURLOPT_FOLLOWLOCATION 的东西吗?在本地主机上尝试了 true 和 false - 仍然有效。在远程托管上,由于某种原因,不允许跟踪位置,但如果它在本地不工作,我认为这并不重要。

<?php
class cURL
{
    private $username;
    private $password;
    private static $tmpfname;

    public function __construct($username,$password) {
        $this->username = $username;
        $this->password = $password;
        $this->makeCookies($username, $password);
    }

    private function makeCookies($username, $password) {
        self::$tmpfname = tempnam("/tmp", "Cookie");
        $useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
        curl_setopt($ch, CURLOPT_COOKIEFILE, self::$tmpfname);
        curl_setopt($ch, CURLOPT_COOKIEJAR, self::$tmpfname);
        curl_setopt($ch, CURLOPT_URL,"http://vk.com/login.php");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "email={$username}&pass={$password}");
        ob_start();
        curl_exec($ch);
        ob_end_clean();
        curl_close($ch);
        unset($ch);
    }

    private function getHTML($url){
        $useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
        curl_setopt($ch, CURLOPT_COOKIEFILE, self::$tmpfname);
        curl_setopt($ch, CURLOPT_COOKIEJAR, self::$tmpfname);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
        $contents = curl_exec($ch);
        curl_close($ch);

        return $contents;
    }

    public function getSearchResults($songname) {
        $songname = urlencode($songname); 
        $contents = $this->getHTML("http://vk.com/search?c[section]=audio&c[q]={$songname}");
        return $contents;
    }
}
?>

cURL returns nothing when on server. Everything works well on localhost, but when it's in remote hosting getSearchResults() returns nothing (or 302 header). Is this something wrong with server configuration (tried 2 different). Can it be something with CURLOPT_FOLLOWLOCATION? Tried both true and false on localhost - still works. On remote hosting it's not allowed to follow location for some reason, but if it works without on local I don't think that matters.

<?php
class cURL
{
    private $username;
    private $password;
    private static $tmpfname;

    public function __construct($username,$password) {
        $this->username = $username;
        $this->password = $password;
        $this->makeCookies($username, $password);
    }

    private function makeCookies($username, $password) {
        self::$tmpfname = tempnam("/tmp", "Cookie");
        $useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
        curl_setopt($ch, CURLOPT_COOKIEFILE, self::$tmpfname);
        curl_setopt($ch, CURLOPT_COOKIEJAR, self::$tmpfname);
        curl_setopt($ch, CURLOPT_URL,"http://vk.com/login.php");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "email={$username}&pass={$password}");
        ob_start();
        curl_exec($ch);
        ob_end_clean();
        curl_close($ch);
        unset($ch);
    }

    private function getHTML($url){
        $useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
        curl_setopt($ch, CURLOPT_COOKIEFILE, self::$tmpfname);
        curl_setopt($ch, CURLOPT_COOKIEJAR, self::$tmpfname);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
        $contents = curl_exec($ch);
        curl_close($ch);

        return $contents;
    }

    public function getSearchResults($songname) {
        $songname = urlencode($songname); 
        $contents = $this->getHTML("http://vk.com/search?c[section]=audio&c[q]={$songname}");
        return $contents;
    }
}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

影子的影子 2025-01-04 00:32:48

302 代码是重定向,因此您需要能够使用 CURLOPT_FOLLOWLOCATION 从中获取任何有用的信息。

A 302 code is a redirect, so you'll need to be able to use CURLOPT_FOLLOWLOCATION to get anything useful out of it.

假扮的天使 2025-01-04 00:32:48

对于在安全模式下运行 php 的 Web 服务器,Web 上有很多重定向机制的实现。例如,此处(您应该查看的第一个地方实际上)是我有一天为自己的脚本修改的。它可以处理多个重定向,并且以您可以轻松理解和修改的方式编写。

There are plenty of implementations of redirecting mechanism on web for web servers that run php in safe mode. For example, here (the first place you should look it for actually) is the one I one day modified for my own script. It can process multiple redirects and is written in a way that you can easily understand and modify it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文