我有一个远程URL输出该图像 的URL是在格式这样如何从php中获取图像?
http://domain.com/my_file/view/<file_id>/FULL/
在URL“my_file”是控制器的名字,“视图”是函数名和另外两个是参数
如果我在浏览器中点击这个网址就说明我的图像
我要采取像在我的项目文件夹
我与的file_get_contents尝试,但它给我提个醒g with 404
我该如何做到这一点?
我有一个远程URL输出该图像 的URL是在格式这样如何从php中获取图像?
http://domain.com/my_file/view/<file_id>/FULL/
在URL“my_file”是控制器的名字,“视图”是函数名和另外两个是参数
如果我在浏览器中点击这个网址就说明我的图像
我要采取像在我的项目文件夹
我与的file_get_contents尝试,但它给我提个醒g with 404
我该如何做到这一点?
使用卷曲此:
功能curlFile($网址) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
function readurl()
{
$url="http://domain.com/my_file/view/<file_id>/FULL/";
curlFile($url);
}
echo readurl();
$img=file_get_contents('http://example.com/image/test.jpg');
file_put_contents('/your/project/folder/imgname.jpg',$img);
如果allow_url_fopen
在php.ini文件设置为1,这仅适用。 如果你可以改变这个值,启用它,你就完成了。
另一个选项是CURL。检查您的PHP配置中是否启用了此模块。
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://example.com/image/test.jpg');
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HEADER , 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$result = @curl_exec($ch);
$curl_err = curl_error($ch);
curl_close($ch);
if (empty($curl_err)) {
file_put_contents('/your/project/folder/imgname.jpg',$result);
}
如果未启用卷曲,你的机会是写这样一个简单的HTTP客户端:如果你想
$image = file_get_contents('http://domain.com/my_file/view/<file_id>/FULL/');
if($image) file_put_contents('some/folder/<file_id>');
$buf='';
$fp = fsockopen('example.com',80);
fputs($fp, "GET /image/test.jpg HTTP/1.1\n");
fputs($fp, "Host: example.com\n");
fputs($fp, "Connection: close\n\n");
while (!feof($fp)) {
$buf .= fgets($fp,128);
}
fclose($fp);
file_put_contents('/your/project/folder/imgname.jpg',$buf);
,它不工作,它大概意思是:
http://domain.com/my_file/view/<file_id>/FULL/
时查看源代码。http://domain.com/my_file/view/<file_id>/FULL
?
给一个真实的网址也许吧? – dachi
如果图像是404,那么它或者不存在,或者您设置的任何限制对它的访问的子句(例如水蛭保护,基于用户代理/引用者的限制)没有得到满足。这张图片在您控制的服务器/主机上吗? – Seidr
给你的真实完整图像网址你的网址必须是http://domain.com/my_file/view/ /FULL/image.png或必须指向图像名称 –