繁体   English   中英

使用curl和file_put_contents PHP从URL保存图像

[英]Save image from url with curl and file_put_contents PHP

我想将图片从远程服务器保存到我的网站。 我在TinyMCE编辑器中创建文本,然后从远程服务器中插入图像。 接下来,我需要将此图片保存到服务器。 为此,我从文本获得了指向图片的链接:

    preg_match('/<img(.*)src(.*)=(.*)"(.*)"/U', $text, $result);
    $url =  array_pop($result);

接下来,通过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);

结果,不会创建图片,而是创建一个文本文件'myfolder / image.jpg',其大小为16 kb,带有文本-错误的URL时间戳。

curl_getinfo returns [content_type] => text/plain [http_code] => 403

但是,如果我在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'

然后文件被成功复制。

curl_getinfo returns [content_type] => image/jpeg [http_code] => 200 

我在哪里做错了?

$_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&amp;oh=6262ebe636e7328f0471af2820fd4050&amp;oe=5C03BEC7" alt="" width="776" height="776" /></p> </body> </html>
)

完整的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);
}}  
?>

$_POST中, img src的内容带有某些特殊字符,例如&编码为&amp;

如果您在浏览器中打开此URL,则会收到相同的错误: https://scontent.ftbs4-1.fna.fbcdn.net/v/t1.0-9/39900479_1856467244440953_5986986678919626752_n.jpg?_nc_cat=0&amp;oh=6262ebe636e7328f0471af2820fd4050&amp;oe=5C03BEC7 : https://scontent.ftbs4-1.fna.fbcdn.net/v/t1.0-9/39900479_1856467244440953_5986986678919626752_n.jpg?_nc_cat=0&amp;oh=6262ebe636e7328f0471af2820fd4050&amp;oe=5C03BEC7

您可以使用html_entity_decode逆转此转义。 如果我更改此行,则卷曲有效:

$img_link = html_entity_decode(array_pop($regexResult));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM