php - cURL 抓取给我 'Request Rejected' 请求的 URL 被拒绝

标签 php curl php-curl

我正在尝试使用这段代码获取网站的产品图片:

<?php

$url="http://www.akasa.com.tw/update.php?tpl=product/cpu.gallery.tpl&type=Fanless Chassis&type_sub=Fanless Mini ITX&model=A-ITX19-A1B";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_USERAGENT, "User-Agent: Mozilla/6.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.7) Gecko/20050414 Firefox/1.0.3");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_ENCODING, "");
$pagebody=curl_exec($ch);

curl_close ($ch);

$html=str_get_html($pagebody);

print_r($html);

PHPStorm 让我读取变量,$pagebody 得到这个值:

<html><head><title>Request Rejected</title></head><body>The requested URL was rejected. If you think this is an error, please contact the webmaster. <br><br>Your support ID is: 4977197659118049932</body></html>

http://www.akasa.com.tw/update.php?tpl=product/cpu.gallery.tpl&type=Fanless Chassis&type_sub=Fanless Mini ITX&model=A-ITX19-A1B

当我使用浏览器时,我可以完美地看到页面,页面源也为我提供了我需要的所有不错的信息,但我想自动从中抓取一些图像。知道如何找出我需要使用 cURL 发送哪些信息,以便网站不会将我视为机器人(我猜这就是问题所在),或者如何找到此类问题的解决方案?

最佳答案

基本上,您需要对查询字符串参数进行编码,以便所有特殊字符都能正确表示到 url 中。您可以使用http_build_query为此,您的 url 构造可能如下所示:

$url = implode('?', [
    'http://www.akasa.com.tw/update.php',
    http_build_query([
        'tpl'      => 'product/cpu.gallery.tpl',
        'type'     => 'Fanless Chassis',
        'type_sub' => 'Fanless Mini ITX',
        'model'    => 'A-ITX19-A1B',
    ])
]);

然后是其余的代码。

关于php - cURL 抓取给我 'Request Rejected' 请求的 URL 被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47612138/

相关文章:

javascript - 如何从 php 中检索 echo 数据到 jquery 1 by 1

php - 循环直到变量达到 0

php - 我应该同时使用 striptags() 和 htmlspecialchars() 来防止 XSS 吗?

php - 如何将Php CURL请求转换为命令行curl

php - centos如何安装curl php

php - 通过 Javascript 进行多图像选择的 HTML 表单

php - 使用 cURL 登录到特定的 WordPress 页面

c - fatal error : curl/curl. h:没有那个文件或目录

linux - Libcurl 未更新

php - php中通过curl上传文件