Ad

How To Return A Single Asset OhMyBrew Laravel Shopify

- 1 answer

I'm trying to get an object in the form of a single Shopify asset in Laravel using ohmybrew/laravel-shopify with this line:

$asset = $shop->api()->rest('GET', '/admin/api/'.ShopifyApp::api()->getVersion().'/themes/80166092888/assets.json?asset[key]=snippets/collection-grid-item.liquid');

I believe the result of $asset should be a Shopify asset but when I print_r($asset) I get this:

stdClass Object ( [errors] => [response] => GuzzleHttp\Psr7\Response Object ( [reasonPhrase:GuzzleHttp\Psr7\Response:private] => OK [statusCode:GuzzleHttp\Psr7\Response:private] => 200 [headers:GuzzleHttp\Psr7\Response:private] => Array ( [Date] => Array ( [0] => Mon, 16 Dec 2019 13:28:09 GMT ) [Content-Type] => Array ( [0] => text/html; charset=utf-8 ) [Transfer-Encoding] => Array ( [0] => chunked ) [Connection] => Array ( [0] => keep-alive ) [Set-Cookie] => Array ( [0] => __cfduid=db02a99a7bcc26406fd2df1c93df79a7e1576502889; expires=Wed, 15-Jan-20 13:28:09 GMT; path=/; domain=.myshopify.com; HttpOnly [1] => _secure_admin_session_id=7e198a5093ed7d91cfd87bda81a73126; path=/admin; expires=Mon, 16 Mar 2020 13:28:09 -0000; secure; HttpOnly [2] => _secure_admin_session_id_csrf=7e198a5093ed7d91cfd87bda81a73126; path=/admin; expires=Mon, 16 Mar 2020 13:28:09 -0000; secure; HttpOnly; SameSite=Lax [3] => _orig_referrer=; Expires=Mon, 30-Dec-19 13:28:09 GMT; Path=/; HttpOnly [4] => _landing_page=%2Fadmin%2Fauth%2Flogin; Expires=Mon, 30-Dec-19 13:28:09 GMT; Path=/; HttpOnly [5] => _master_udr=eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaEpJaWszTTJGbVl6VmlaaTA0TlRoa0xUUmtOemN0T0RZNU55MWxZV0l6WVRnM05HVmtObVlHT2daRlJnPT0iLCJleHAiOiIyMDIxLTEyLTE2VDEzOjI4OjA5LjU3OVoiLCJwdXIiOiJjb29raWUuX21hc3Rlcl91ZHIifX0%3D--e71a00abb5d0a41d5af672a647e68b4306ceea78; domain=.myshopify.com; path=/admin; expires=Thu, 16 Dec 2021 13:28:09 -0000; secure; HttpOnly ) [X-Sorting-Hat-PodId] => Array ( [0] => 87 ) [X-Sorting-Hat-ShopId] => Array ( [0] => 8865972324 ) [Vary] => Array ( [0] => Accept-Encoding ) [Referrer-Policy] => Array ( [0] => origin-when-cross-origin ) [X-Frame-Options] => Array ( [0] => DENY ) [X-ShopId] => Array ( [0] => 8865972324 ) [X-ShardId] => Array ( [0] => 87 ) [Cache-Control] => Array ( [0] => no-cache, no-store ) [X-Shopify-Login-Required] => Array ( [0] => true ) [Shopify-Auth-Mechanisms] => Array ( [0] => password ) [Strict-Transport-Security] => Array ( [0] => max-age=7889238 ) [X-Shopify-Stage] => Array ( [0] => production ) [Content-Security-Policy] => Array ( [0] => default-src 'self' data: blob: 'unsafe-inline' 'unsafe-eval' https://* shopify-pos://; block-all-mixed-content; child-src 'self' https:// shopify-pos://; connect-src 'self' wss:// https://*; frame-ancestors 'none'; img-src 'self' data: blob: https:; script-src https://cdn.shopify.comhttps://cdn.shopify.cnhttps://js-agent.newrelic.comhttps://bam.nr-data.nethttps://v.shopify.com 'unsafe-inline' 'unsafe-eval'; upgrade-insecure-requests; report-uri /csp-report?source%5Baction%5D=login&source%5Bapp%5D=Shopify&source%5Bcontroller%5D=admin%2Fauth&source%5Bsection%5D=admin&source%5Buuid%5D=8f3c44cc-7e32-48f5-a018-80df31681bc9 ) [X-Content-Type-Options] => Array ( [0] => nosniff ) [X-Download-Options] => Array ( [0] => noopen ) [X-Permitted-Cross-Domain-Policies] => Array ( [0] => none ) [X-XSS-Protection] => Array ( [0] => 1; mode=block; report=/xss-report?source%5Baction%5D=login&source%5Bapp%5D=Shopify&source%5Bcontroller%5D=admin%2Fauth&source%5Bsection%5D=admin&source%5Buuid%5D=8f3c44cc-7e32-48f5-a018-80df31681bc9 ) [X-Dc] => Array ( [0] => gcp-us-east1,gcp-us-central1,gcp-us-central1 ) [NEL] => Array ( [0] => {"report_to":"network-errors","max_age":2592000,"failure_fraction":0.01,"success_fraction":0.0001} [1] => {"report_to":"network-errors","max_age":2592000,"failure_fraction":0.01,"success_fraction":0.0001} ) [Report-To] => Array ( [0] => {"group":"network-errors","max_age":2592000,"endpoints":[{"url":"https://monorail-edge.shopifycloud.com/v1/reports/nel/20190325/shopify"}]} [1] => {"group":"network-errors","max_age":2592000,"endpoints":[{"url":"https://monorail-edge.shopifycloud.com/v1/reports/nel/20190325/shopify"}]} ) [X-Request-ID] => Array ( [0] => 8f3c44cc-7e32-48f5-a018-80df31681bc9 ) [CF-Cache-Status] => Array ( [0] => DYNAMIC ) [Expect-CT] => Array ( [0] => max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" ) [Server] => Array ( [0] => cloudflare ) [CF-RAY] => Array ( [0] => 5460ffb34d00dc0b-LHR ) ) [headerNames:GuzzleHttp\Psr7\Response:private] => Array ( [date] => Date [content-type] => Content-Type [transfer-encoding] => Transfer-Encoding [connection] => Connection [set-cookie] => Set-Cookie [x-sorting-hat-podid] => X-Sorting-Hat-PodId [x-sorting-hat-shopid] => X-Sorting-Hat-ShopId [vary] => Vary [referrer-policy] => Referrer-Policy [x-frame-options] => X-Frame-Options [x-shopid] => X-ShopId [x-shardid] => X-ShardId [cache-control] => Cache-Control [x-shopify-login-required] => X-Shopify-Login-Required [shopify-auth-mechanisms] => Shopify-Auth-Mechanisms [strict-transport-security] => Strict-Transport-Security [x-shopify-stage] => X-Shopify-Stage [content-security-policy] => Content-Security-Policy [x-content-type-options] => X-Content-Type-Options [x-download-options] => X-Download-Options [x-permitted-cross-domain-policies] => X-Permitted-Cross-Domain-Policies [x-xss-protection] => X-XSS-Protection [x-dc] => X-Dc [nel] => NEL [report-to] => Report-To [x-request-id] => X-Request-ID [cf-cache-status] => CF-Cache-Status [expect-ct] => Expect-CT [server] => Server [cf-ray] => CF-RAY ) [protocol:GuzzleHttp\Psr7\Response:private] => 1.1 [stream:GuzzleHttp\Psr7\Response:private] => GuzzleHttp\Psr7\Stream Object ( [stream:GuzzleHttp\Psr7\Stream:private] => Resource id #15 [size:GuzzleHttp\Psr7\Stream:private] => [seekable:GuzzleHttp\Psr7\Stream:private] => 1 [readable:GuzzleHttp\Psr7\Stream:private] => 1 [writable:GuzzleHttp\Psr7\Stream:private] => 1 [uri:GuzzleHttp\Psr7\Stream:private] => php://temp [customMetadata:GuzzleHttp\Psr7\Stream:private] => Array ( ) ) ) [body] => [link] => [timestamps] => Array ( [0] => [1] => 1576502889.2041 ) )

I can correctly retrieve a list of all assets using:

$assets = $shop->api()->rest('GET', '/admin/api/'.ShopifyApp::api()->getVersion().'/themes/80166092888/assets.json');

but this object doesn't include the information I need - in particular, the value of each asset.

The Shopify documentation details this is the correct way to retrieve a single asset:

GET /admin/api/2019-10/themes/#{theme_id}/assets.json?asset[key]=templates/index.liquid&theme_id=828155753

and I can successfully make a GET request from the browser to retrieve the information I want.

Not sure what I'm doing wrong on the PHP/Laravel/OhMyBrew side of things.

Ad

Answer

I found the answer in the docs for ohmybrew/Basic-Shopify-API which is the API wrapper implemented in the laravel-shopify package. The url parameter needs to be passed as a separate parameter to the rest method, like so:

$assets = $shop->api()->rest('GET', '/admin/api/'.ShopifyApp::api()->getVersion().'/themes/80166092888/assets.json', ['asset[key]' => 'snippets/collection-grid-item.liquid']);

Hope that helps anyone else struggling with the same problem.

Ad
source: stackoverflow.com
Ad