How can I get image from url in PHP or Jquery or in both?

前端 未结 5 1548
萌比男神i
萌比男神i 2021-01-29 14:24

Can anybody tell me, how can I get image for a particular site by its url only? As that is done on the google search page. I have searched web but I didn\'t get any satisfactory

5条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-29 15:25

    Get the page with file_get_contents($url) then you can parse the content and download the images and store it at your server:

    $url = 'http://example.com/image.php';
    $img = '/my/folder/flower.gif';
    file_put_contents($img, file_get_contents($url));
    

    See this related Question.

    Update: To get the image you could use SimpleXMLElement:

    $obj = new SimpleXMLElement($htmlCode);
    $url = $obj->body->img->src;
    

提交回复
热议问题