当前位置:  开发笔记 > 编程语言 > 正文

PHP按图搜索谷歌cURL返回302移动

如何解决《PHP按图搜索谷歌cURL返回302移动》经验,为你挑选了2个好方法。

我正在尝试获取一个简单的字符串,其中包含我用逐个图像搜索的图像的描述.所以我设置了search_by_google.php页面:

    ]+>([^<]+)/', $searched, $matches);
        return (count($matches) > 1 ? $matches[1] : false);
    }
 }
  ?>

我已经改变了所有卷曲选项但是如果我去http://www.mysite.altervista.org/search_by_google.php?url=http://www.mysite.org/asdasd.jpg&raw=false

它让我说302 Moved

在此输入图像描述

我改变了我的代码

    curl_setopt ($ch, CURLOPT_HEADER, TRUE);

在第二个curl_init()中,现在它给了我这条消息:

在此输入图像描述

编辑25/03/2014 19:34

我改变了我的代码,如Sabuj Hassan所说,现在的日志是:

HTTP/1.0 302 Found Cache-Control: public, max-age=21600 Date: Tue, 25 Mar 2014 18:30:07 GMT Age: 16 Location: http://www.google.com/search?tbs=sbi:AMhZZisAo2ZcfY19aFUJcEj26M4zKc9ZuxzfsUPzLuUJk-pd-siPwiplqIcGN5tW1XPU16-XFg1EoK7jc5IU3BKoEHYnwZo7RmuhyF5p9qaZwSgq4FKRkNW44JgzTi4Mr8g6ezNMQ6YzaAEQ-uFbPMNzY40NrE3uB7ePm4BGNowF34PiIjLOiVLkWwQ7sRoBVMoVgzBbAP7rDwHee5LyGF8Dq6QOT1TEhsURduPD6exzITyRl77agELdpTFSi-JXDncI6c4KdcuQYSx2LknnIW6nippmpPf3X5OYGn1CFZw13rlFPitLSY0Ang0COuSXKdpBy6B8Dak9QZNZ9VFB4HBRfnMFiyuBvQtyhAg2LeOnRbjnunGB0P1RlwKBF4hRId7wUdTu4Dfab5DQu9hGauLKcd7GcP4g-jQXx_1gymwDdZnPXLzZp1mkjVMX9GFSppj-IRWp3FVVqChsPEzKXdraevuWJFukjUdF87dU_1kLKO23lC8L3kusy05zcq7ZxyF1dHNfQ0vYJeWumtbRosJNuEcqiSyVW_1-bF104HMJLdCA0gr5VyIZolkcZok4W1sgjFYTWvfj6f0proaGE24HSO4Ov2hmhAy9HQUCr3e-KjgqyP4AOtlmI3VsuLu34jKSo0t4tWbb5PVBi1_1oebuv4oisdVdw22a6CRH2tiw8wg6Ya1VgxsXhyj8U7lrQ8cBHVDKlOI6EimXtnELBHyDNQT1Zpsz1hK10GYvFaRNMFd7Rqmg87CLdycgyRV-sYxNWxIu9agNgHTwuU1W-GgeWWcM9noeMwgqMKSGh9lt_1hda3ZWrcA4Y1MeiG55b4ZYvOjcm9t9iIy6LA2S4AjC2X1qZHvJtSqzgfOz8yTuX5jUHqCl0jI1FdOSmqZV1GqQ0uaJfsuchlsWUULfUJBzFiGkAuOqIzU0bpXLNqLHoYPJUPwr66H6jWPFLsWAS9_1GRNj70s30jfbzcS0NUShUvE2meUhlpx-f5M0nmS0zvf-3OQOUkXlYO2VUZ4x9y8G76hHoTkDxqzhhGrgohyFmkUvAWmSkHTBpbP6gek8cyrmBnXuedSV3r2O71G8CUbdHFxfIO8FWlkGj1cUYu60PoKF6hndjZsOlV-dSNXfOTKeC1jPtf5ycXA0s0xLK7_1K0iWxhfmVq62WgQ4O3Prc4b6bcJm8M1Q9xZhhsElisuUyVTN9-dDMNUZ1h0tUe9oGsZYLh9vjEsMokqBXFM_1igHOfgRn4I17Xt8EBMZI9cEjakByjv-g5Pt9tG69RQm765HLhf8VpafvE5Z3BwDpZs4x5uMkVDURT9qcA&hl=en Server: quimby_frontend Content-Length: 1566 Content-Type: text/html; charset=UTF-8 Expires: Wed, 26 Mar 2014 00:30:07 GMT Alternate-Protocol: 80:quic X-Content-Type-Options: nosniff X-Frame-Options: SAMEORIGIN X-XSS-Protection: 1; mode=block
302 Moved

The document has moved here. HTTP/1.0 302 Found Cache-Control: public, max-age=21600 Date: Tue, 25 Mar 2014 18:30:07 GMT Age: 16 Location: http://www.google.com/search?tbs=sbi:AMhZZisAo2ZcfY19aFUJcEj26M4zKc9ZuxzfsUPzLuUJk-pd-siPwiplqIcGN5tW1XPU16-XFg1E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xLK7_1K0iWxhfmVq62WgQ4O3Prc4b6bcJm8M1Q9xZhhsElisuUyVTN9-dDMNUZ1h0tUe9oGsZYLh9vjEsMokqBXFM_1igHOfgRn4I17Xt8EBMZI9cEjakByjv-g5Pt9tG69RQm765HLhf8VpafvE5Z3BwDpZs4x5uMkVDURT9qcA&hl=en Server: quimby_frontend Content-Length: 1566 Content-Type: text/html; charset=UTF-8 Expires: Wed, 26 Mar 2014 00:30:07 GMT Alternate-Protocol: 80:quic X-Content-Type-Options: nosniff X-Frame-Options: SAMEORIGIN X-XSS-Protection: 1; mode=block
302 Moved

The document has moved here.

Sabuj Hassan.. 7

可能会发生以下情况:在您的服务器上为您的卷曲阻止了重定向.所以我建议你手动处理重定向.像这个:

首先你的卷曲功能.如果您愿意,可以添加其他卷曲选项:

function curl($url, $user_agent, $retry=0){
    if($retry > 5){
        print "Maximum 5 retries are done, skipping!\n";
        return "in loop!";
    }

    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
    curl_setopt ($ch, CURLOPT_HEADER, TRUE);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt ($ch, CURLOPT_REFERER, 'http://www.google.com/');
    curl_setopt ($ch, CURLOPT_COOKIEFILE,"./cookie.txt");
    curl_setopt ($ch, CURLOPT_COOKIEJAR,"./cookie.txt");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $result = curl_exec($ch);
    curl_close($ch);

    // handling the follow redirect
    if(preg_match("|Location: (https?://\S+)|", $result, $m)){
        print "Manually doing follow redirect!\n$m[1]\n";
        return curl($m[1], $user_agent, $retry + 1);
    }

    // add another condition here if the location is like Location: /home/products/index.php

    return $result;
}

这是应该如何调用:

$response = curl("http://www.google.com/", "Mozilla 5.0");
print "$response\n";

我正在解析Location:标题中的以下链接.可能会发生链接未启动的http://情况.那个案例在那里添加另一个条件.



1> Sabuj Hassan..:

可能会发生以下情况:在您的服务器上为您的卷曲阻止了重定向.所以我建议你手动处理重定向.像这个:

首先你的卷曲功能.如果您愿意,可以添加其他卷曲选项:

function curl($url, $user_agent, $retry=0){
    if($retry > 5){
        print "Maximum 5 retries are done, skipping!\n";
        return "in loop!";
    }

    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
    curl_setopt ($ch, CURLOPT_HEADER, TRUE);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt ($ch, CURLOPT_REFERER, 'http://www.google.com/');
    curl_setopt ($ch, CURLOPT_COOKIEFILE,"./cookie.txt");
    curl_setopt ($ch, CURLOPT_COOKIEJAR,"./cookie.txt");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $result = curl_exec($ch);
    curl_close($ch);

    // handling the follow redirect
    if(preg_match("|Location: (https?://\S+)|", $result, $m)){
        print "Manually doing follow redirect!\n$m[1]\n";
        return curl($m[1], $user_agent, $retry + 1);
    }

    // add another condition here if the location is like Location: /home/products/index.php

    return $result;
}

这是应该如何调用:

$response = curl("http://www.google.com/", "Mozilla 5.0");
print "$response\n";

我正在解析Location:标题中的以下链接.可能会发生链接未启动的http://情况.那个案例在那里添加另一个条件.



2> 小智..:

问题可能出在CURLOPT_FOLLOWLOCATION,如果在php.ini中启用了safe_mode或open_basedir,则该URL不可用

试试这个答案:http : //au.php.net/manual/ro/function.curl-setopt.php#71313

只需用注释中提供的curl_redir_exec()替换curl_exec()即可。

推荐阅读
贴进你的心聆听你的世界
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有