[英]Save image from url with curl and file_put_contents PHP
i want to save a picture from a remote server to my site. 我想将图片从远程服务器保存到我的网站。 I create the text in the TinyMCE editor and insert the image from the remote server there. 我在TinyMCE编辑器中创建文本,然后从远程服务器中插入图像。 Next, I need to save this picture to my server. 接下来,我需要将此图片保存到服务器。 To do this, i get a link to the picture from the text: 为此,我从文本获得了指向图片的链接:
preg_match('/<img(.*)src(.*)=(.*)"(.*)"/U', $text, $result);
$url = array_pop($result);
Next through curl and file_put_contents
i get the file and copy to my server. 接下来,通过curl和file_put_contents
我得到文件并复制到我的服务器。
$headers = array();
$headers[] = 'Content-Type: image/jpeg';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url ) ;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01;
Windows NT 5.0)");
$image = curl_exec($ch);
curl_close($ch);
file_put_contents('myfolder/image.jpg', $url);
As a result, not a picture is created, but a text file 'myfolder/image.jpg' with a size of 16 kb with text - Bad URL timestamp. 结果,不会创建图片,而是创建一个文本文件'myfolder / image.jpg',其大小为16 kb,带有文本-错误的URL时间戳。
curl_getinfo returns [content_type] => text/plain [http_code] => 403
But, in case i am assigning $url
manually in CURLOPT_URL
, for example 但是,如果我在CURLOPT_URL
手动分配$url
,例如
$url = 'https://scontent.ftbs4-1.fna.fbcdn.net/v/t1.0-9/39900479_1856467244440953_5986986678919626752_n.jpg?_nc_cat=0&oh=6262ebe636e7328f0471af2820fd4050&oe=5C03BEC7'
then the file is successfully copied. 然后文件被成功复制。
curl_getinfo returns [content_type] => image/jpeg [http_code] => 200
Where did I do wrong? 我在哪里做错了?
This $_POST
: $_POST
:
Array (
[id] => 143
[title] => Topic
[description] => description
[text] => <!DOCTYPE html> <html> <head> </head> <body> <p>Hello</p> <p><img src="https://scontent.ftbs4-1.fna.fbcdn.net/v/t1.0-9/39900479_1856467244440953_5986986678919626752_n.jpg?_nc_cat=0&oh=6262ebe636e7328f0471af2820fd4050&oe=5C03BEC7" alt="" width="776" height="776" /></p> </body> </html>
)
full php code 完整的php代码
<?php
//print_r($_POST);
preg_match_all('/<img[^>]+>/i',$_POST['text'] , $result);
foreach($result as $img_tag){
foreach( $img_tag as $tag){
preg_match('/<img(.*)src(.*)=(.*)"(.*)"/U', $tag, $regexResult);
$img_link = array_pop($regexResult);
$file_name = basename($img_link);
//$img_link = 'https://scontent.ftbs4-1.fna.fbcdn.net/v/t1.0-9/39900479_1856467244440953_5986986678919626752_n.jpg?_nc_cat=0&oh=6262ebe636e7328f0471af2820fd4050&oe=5C03BEC7';
$headers = array();
$headers[] = 'Content-Type: image/jpeg';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $img_link ) ;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
$html = curl_exec($ch);
curl_close($ch);
$targetPath = '/folder/'.$_POST['id'].'/';
file_put_contents($targetPath.$file_name, $html);
}}
?>
In your $_POST
the content of the img src
is coming in with certain special characters like &
encoded as &
在$_POST
中, img src
的内容带有某些特殊字符,例如&
编码为&
. 。
If you open this URL in the browser, you get the same error: https://scontent.ftbs4-1.fna.fbcdn.net/v/t1.0-9/39900479_1856467244440953_5986986678919626752_n.jpg?_nc_cat=0&oh=6262ebe636e7328f0471af2820fd4050&oe=5C03BEC7
. 如果您在浏览器中打开此URL,则会收到相同的错误: https://scontent.ftbs4-1.fna.fbcdn.net/v/t1.0-9/39900479_1856467244440953_5986986678919626752_n.jpg?_nc_cat=0&oh=6262ebe636e7328f0471af2820fd4050&oe=5C03BEC7
: https://scontent.ftbs4-1.fna.fbcdn.net/v/t1.0-9/39900479_1856467244440953_5986986678919626752_n.jpg?_nc_cat=0&oh=6262ebe636e7328f0471af2820fd4050&oe=5C03BEC7
。
You can reverse this escaping using html_entity_decode
. 您可以使用html_entity_decode
逆转此转义。 If I change this line the curl works: 如果我更改此行,则卷曲有效:
$img_link = html_entity_decode(array_pop($regexResult));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.