我有一段代码,旨在接收任何网址并将其从网上删除。到目前为止它一直工作正常,直到有人给它这个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);
他们的服务器是否意识到我不是普通的浏览器并启动我?
答案 0 :(得分:8)
他们经常检查你是谁。添加普通浏览器的useragent,你应该没问题。
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0");
以下是codepad中的工作示例。
答案 1 :(得分:2)
某些服务器为了阻止不必要的流量,只允许从浏览器下载。因此,为了欺骗这些服务器,curl还有一个额外的选项--user-agent,可以解决这个问题!
我在windows7 PC上使用curl,安装了gow。
实施例
curl --user-agent "Mozilla/4.0" http://www.example.com/archives/abc.txt --output pqr.txt
答案 2 :(得分: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);
}
}
?>
和accept.php
<?php
if(isset($_FILES['myfile']['tmp_name'])) {
$path = "myfiles/" . $_FILES['myfile']['name'];
move_uploaded_file($_FILES['myfile']['tmp_name'], $path);
}
?>