【问题标题】:facebook image save curlfacebook图像保存卷曲
【发布时间】:2016-07-14 05:21:43
【问题描述】:

保存图片的大小 0 kb

工作代码,除了 facebook

 function imagedownload($url,$saveto){
                $ch = curl_init ($url);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
                curl_setopt($ch, CURLOPT_HEADER, false);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
                curl_setopt($ch, CURLOPT_POST, true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
                $result = parse_url($url);
                curl_setopt($ch, CURLOPT_REFERER, $result['scheme'].'://'.$result['host']);
                curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0');
                $raw=curl_exec($ch);
                curl_close ($ch);
                if(file_exists($saveto)){
                unlink($saveto);
                }
                $fp = fopen($saveto,'x');
                fwrite($fp, $raw);
                fclose($fp);
                }

                $url="http://scontent-frt3-1.xx.fbcdn.net/v/t1.0-0/cp0/e15/q65/p320x320/13700038_850487491753797_6227258625184891703_n.jpg?oh=793ecde8db1a8e65789534907d08b25e&oe=57F1DDFF";

                $konum="images/"

                $yolla=imagedownload($url,$konum);

保存图片的大小 0 kb

工作代码,除了 facebook

【问题讨论】:

    标签: php facebook curl get


    【解决方案1】:

    如果您删除 POST 的选项并执行常规的 GET 请求,它会起作用:

    <?php
    
    function imagedownload($url, $saveto)
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        $result = parse_url($url);
        curl_setopt($ch, CURLOPT_REFERER, $result['scheme'] . '://' . $result['host']);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0');
        $raw = curl_exec($ch);
        curl_close($ch);
        if (file_exists($saveto)) {
            unlink($saveto);
        }
        $fp = fopen($saveto, 'x');
        fwrite($fp, $raw);
        fclose($fp);
    }
    
    $url = "http://scontent-frt3-1.xx.fbcdn.net/v/t1.0-0/cp0/e15/q65/p320x320/13700038_850487491753797_6227258625184891703_n.jpg?oh=793ecde8db1a8e65789534907d08b25e&oe=57F1DDFF";
    $konum = "test.jpg";
    
    $yolla = imagedownload($url, $konum);
    

    【讨论】:

    • 但问题仍然存在
    • 它对我有用,所以请尝试启用CURLOPT_VERBOSE,然后看看它说了什么。
    【解决方案2】:

    我已经在我的本地主机中尝试了这段代码,它运行良好。

    <?php
    ini_set('display_errors', 1);
    function imagedownload($url,$saveto){
        $ch = curl_init ($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
        $raw=curl_exec($ch);
        curl_close ($ch);
        if(file_exists($saveto)){
        unlink($saveto);
        }
        $fp = fopen($saveto,'x');
        fwrite($fp, $raw);
        fclose($fp);
    }
    $url="http://scontent-frt3-1.xx.fbcdn.net/v/t1.0-0/cp0/e15/q65/p320x320/13700038_850487491753797_6227258625184891703_n.jpg?oh=793ecde8db1a8e65789534907d08b25e&oe=57F1DDFF";
    $konum="/var/www/html/jsPDF-master/examples/test.jpg";
    $yolla=imagedownload($url,$konum);
    ?>
    

    结果

    还有一点注意:请确保保存图片的目录必须有写权限。

    示例:chmod -R 777 /var/www/html/jsPDF-master/examples

    或者确保在 php.ini 中启用 allow_url_fopen

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-24
      • 2014-05-26
      相关资源
      最近更新 更多