Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
Hi everyone,
I'm experiencing an issue when trying to download images from my Shopify store using the API. I have a valid access token for my store and I'm using GuzzleHttp\Client to send requests.
Specifically, I'm trying to download images from URLs like https://cdn.shopify.com/s/files/1/.../.../.../files/[IMAGE_NAME].jpg
The problem is that Shopify seems to be blocking automatic image downloads through the API. I've tried different approaches, including using GraphQL API and REST interface, but nothing seems to work.
Can anyone help me troubleshoot this issue? How can I download images from my Shopify store using the API? I need to keep them locally
Any solution should be safe and reliable.
$this->apiUrl = config('services.shopify.api_url');
$this->client = new Client([
'headers' => [
'Content-Type' => 'application/json',
'X-Shopify-Access-Token' => config('services.shopify.access_token'),
],
]);
Then I'm retrieving product data with images. Image urls stored as json in database
All links are correct and can be accessed in the browser
but then I need to download them to local storage:
private function download(array $imageUrls): void
{
foreach ($imageUrls as $url) {
if (! File::exists('./storage/store-images/'.auth()->id())) {
File::makeDirectory('./storage/store-images/'.auth()->id(), 0777, true);
}
$query = "query { image(url: \"$url\") }";
$response = $this->client->post($this->apiUrl , [
'headers' => ['Content-Type' => 'application/json'],
'json' => ['query' => $query],
]);
if ($response->getStatusCode() === 200) {
$responseData = json_decode((string)$response->getBody(), true);
if (isset($responseData['data']['image'])) {
File::put('./storage/store-images/'.basename($url), (string)$responseData['data']['image']);
} else {
logger()->info('Could not download image', (array)$response->getStatusCode());
}
} else {
logger()->info('Could not download image', (array)$response->getStatusCode());
}
}
}
Thanks in advance for your help!
Solved
Hey Community! As we jump into 2025, we want to give a big shout-out to all of you wh...
By JasonH Jan 7, 2025Hey Community! As the holiday season unfolds, we want to extend heartfelt thanks to a...
By JasonH Dec 6, 2024Dropshipping, a high-growth, $226 billion-dollar industry, remains a highly dynamic bus...
By JasonH Nov 27, 2024