Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP从URL复制图像_Php_Image_Url - Fatal编程技术网

使用PHP从URL复制图像

使用PHP从URL复制图像,php,image,url,Php,Image,Url,我想使用PHP将图像从url复制到我的服务器。 我看到了这个答案 并尝试了以下方法: <?php $url = 'https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xap1/v/t1.0-1/p200x200/548412_147785902099377_260065314_n.jpg?oh=5c97dcd58931398e501666daee4c4ae8&oe=5457AC73&__gda__=1414911437_494

我想使用PHP将图像从url复制到我的服务器。 我看到了这个答案 并尝试了以下方法:

<?php

$url = 'https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xap1/v/t1.0-1/p200x200/548412_147785902099377_260065314_n.jpg?oh=5c97dcd58931398e501666daee4c4ae8&oe=5457AC73&__gda__=1414911437_494ad1af138ee7670f89f4a6ba8b6d06';
$img = 'flower.jpg';
copy($url, $img);

?>
我的php版本是5.4,在我的php.ini中允许\u url\u fopen=On 我找不到如何解决这个问题,有什么聪明的建议吗?

我可以在命令行中通过简单的curl抓住它:

curl "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xap1/v/t1.0-1/p200x200/548412_147785902099377_260065314_n.jpg?oh=5c97dcd58931398e501666daee4c4ae8&oe=5457AC73&__gda__=1414911437_494ad1af138ee7670f89f4a6ba8b6d06" > bill.jpg

也许您可以使用php的系统来实现这一点?

使用文件内容而不是复制。使用下面的代码

file_put_contents($img, file_get_contents($url));

使用curl怎么样,然后可以直接写入文件

<?php
$url = 'https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xap1/v/t1.0-1/p200x200/548412_147785902099377_260065314_n.jpg?oh=5c97dcd58931398e501666daee4c4ae8&oe=5457AC73&__gda__=1414911437_494ad1af138ee7670f89f4a6ba8b6d06';

$fp = fopen ('./flower.jpg', 'w+');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, 'YourBot.0.1');
curl_setopt($ch, CURLOPT_FILE, $fp); // write curl response to file
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  false); //required!
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,  false); //required!
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>

也许你可以使用文件获取内容?正如您链接到的问题的公认答案一样。该服务器不喜欢您以这种方式访问其文件。-同时,取下https中的s,看看是否有帮助。@CompuChip-这与底层机制完全相同。@Deryck相同的错误仍然存在。相同的错误仍然存在。奇怪……这里没有问题。我已经测试过了,它可以工作。如果fb真的阻止了你尝试的主机,如果你在本地进行测试,请检查你是否至少可以在浏览器中查看图像。同时检查你是否安装了curl
<?php
$url = 'https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xap1/v/t1.0-1/p200x200/548412_147785902099377_260065314_n.jpg?oh=5c97dcd58931398e501666daee4c4ae8&oe=5457AC73&__gda__=1414911437_494ad1af138ee7670f89f4a6ba8b6d06';

$fp = fopen ('./flower.jpg', 'w+');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, 'YourBot.0.1');
curl_setopt($ch, CURLOPT_FILE, $fp); // write curl response to file
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  false); //required!
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,  false); //required!
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>