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;
?>
6
  • 1
    Can we see your code? Commented Feb 28, 2019 at 14:46
  • We need some code here Commented Feb 28, 2019 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. Commented Feb 28, 2019 at 14:48
  • 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.) Commented Feb 28, 2019 at 14:50
  • 1
    Possible duplicate of php: recreate and display an image from binary data Commented Feb 28, 2019 at 14:54

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.