0

I have the following url which returns an image in the body, How would I assign that image to a variable so that I could save it or use it within my code?

https://media.licdn.com/dms/image/C4E03AQGOM3p0fHNkwQ/profile-displayphoto-shrink_100_100/0?e=1556755200&v=beta&t=qeQFKYXpev2ZW3hmP1ODDPd3DYPWvl-GaUnPSZG-aQA

The following code returns noting:

<?php
     $url = "https://media.licdn.com/dms/image/C4E03AQGOM3p0fHNkwQ/profile-displayphoto-shrink_100_100/0?e=1556755200&v=beta&t=qeQFKYXpev2ZW3hmP1ODDPd3DYPWvl-GaUnPSZG-aQA";
     $image = file_get_contents($url);
     echo $image;
?>
MattBlack
  • 3,616
  • 7
  • 32
  • 58
  • 1
    Can we see your code? – Baransel A. Feb 28 '19 at 14:46
  • We need some code here – Russ J Feb 28 '19 at 14:47
  • 1
    `How would I assign that image to a variable so that I could save it or use it within my code` --- you already did. – specializt Feb 28 '19 at 14:48
  • What does a `var_dump($image);` show you? – Dave Feb 28 '19 at 14:50
  • 1
    Such a CDN might reject requests it does not think come from a “regular” browser … so I’d start with specifying a User-Agent request header that mimics that of a current, popular browser. (If you don’t know how to do that using file_get_contents, please research.) – 04FS Feb 28 '19 at 14:50
  • 1
    Possible duplicate of [php: recreate and display an image from binary data](https://stackoverflow.com/questions/2070603/php-recreate-and-display-an-image-from-binary-data) – Motassem Kassab Feb 28 '19 at 14:54

1 Answers1

2

If you simply want to view an image from a URL, just put that url in the src attribute of an img element like this :
echo '<img src="' . $url . '"/>';

otherwise, if you have to view it from the fetched data of that url, then your question have been answered here:
php: recreate and display an image from binary data (specifically @Krab's answer)

Motassem Kassab
  • 1,744
  • 4
  • 21
  • 40