5

我有一段代码旨在接收任何 URL 并将其从网络上删除。到目前为止,它一直运行良好,直到有人给它这个 URL:

http://www.aspensurgical.com/static/images/aspen_hill-rom_logo.png

如果我从浏览器中点击它,它显示得很好。但是当我尝试将它卷曲时,我得到:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /static/images/aspen_hill-rom_logo.png
on this server.</p>
<hr>
<address>  Server at www.aspensurgical.com Port 80</address>
</body></html>

我正在使用的 CURL 代码是:

$ch = curl_init(str_replace(' ', '%20', $url));
$fh = fopen($local_file, "w");
curl_setopt($ch, CURLOPT_FILE, $fh);
curl_exec($ch);
curl_close($ch);

他们的服务器是否以某种方式意识到我不是普通的浏览器并引导我?

4

3 回答 3

9

他们有 useragent 检查你是谁。添加普通浏览器的用户代理,你应该没问题。

curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0");

这是codepad中的工作示例。

于 2013-03-21T06:03:45.893 回答
2

一些服务器为了阻止不必要的流量,只允许从浏览器下载。所以,为了欺骗这样的服务器,curl 有一个额外的选项 --user-agent ,它可以解决问题!

我在我的 windows7 PC 上使用 curl,已经安装了 gow。

例子

curl --user-agent "Mozilla/4.0" http://www.example.com/archives/abc.txt --output pqr.txt
于 2015-09-18T09:39:03.557 回答
0

我有同样的问题修复

<form action="gc.php" method="post" enctype="multipart/form-data">
<input type="file" name="f">
<input type="submit" value="Post">
</form>

    <?php
    if(isset($_FILES['f']['tmp_name'])) {
    $ch = curl_init();

    $cfile = new CURLFile($_FILES['f']['tmp_name'], $_FILES['f']['type'], 
    $_FILES['f']['name']);
    $data = array("myfile" =>$cfile);


    curl_setopt($ch, CURLOPT_URL, "http://your-url/accept.php");
    curl_setopt($ch, CURLOPT_HEADER, true);
            curl_setopt($ch, CURLOPT_NOBODY, true);
            curl_setopt($ch, CURLOPT_REFERER, $url);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($ch, CURLOPT_MAXREDIRS, 15);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
            curl_setopt($ch, CURLOPT_FAILONERROR, true);

    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    $response = curl_exec($ch);

    if($response == true) {
        echo "File posted";
    } else { 
        echo "Error" . curl_error($ch);
    }
    }
    ?>

并接受.php

   <?php
     if(isset($_FILES['myfile']['tmp_name'])) {
     $path = "myfiles/" . $_FILES['myfile']['name'];
     move_uploaded_file($_FILES['myfile']['tmp_name'], $path);
     }

     ?>
于 2018-04-27T13:12:35.623 回答