I have a third-party API that returns a JSON response when fetching an image file. The response has a key named content. This key contains the binary data of the image asked for.
I get this response using cURL in PHP, then json_decode
the response.
I get the content key, get the contents out of that and save it in a variable.
Then, I pass this string to imagecreatefromstring
but it always returns false.
What's wrong here? How can I convert that binary data into an image resource?
If anyone has a shortcut, ultimately, I need to store that image at a specific location on the File System.
The response that I get is here: https://gist.github.com/abhisheksoni27/2949f42eab5851ad75e77026fe53be87
<?php
$file_name = 'FILE_NAME_HERE';
$url = API_URL_HERE . $file_name;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec($ch);
$resJson = json_decode($response, true);
$image_content = $resJson["content"];
$image = imagecreatefromstring($image_content);
var_dump($image); // this is always false