如何 return 单一资产 OhMyBrew Laravel Shopify

How to return a single asset OhMyBrew Laravel Shopify

我正在尝试使用 ohmybrew/laravel-shopify 在 Laravel 中获取单个 Shopify 资产形式的对象:

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

我相信 $asset 的结果应该是 Shopify 资产但是当我 print_r($asset) 我得到这个:

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.com https://cdn.shopify.cn https://js-agent.newrelic.com https://bam.nr-data.net https://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 ) )

我可以使用以下方法正确检索所有资产的列表:

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

但是这个对象不包含我需要的信息——尤其是每项资产的价值。

Shopify 文档详细说明这是检索单个资产的正确方法:

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

并且我可以成功地从浏览器发出 GET 请求以检索我想要的信息。

不确定我在 PHP/Laravel/OhMyBrew 方面做错了什么。

我在 ohmybrew/Basic-Shopify-API 的文档中找到了答案,它是 laravel-shopify 包中实现的 API 包装器。 url参数需要作为单独的参数传递给rest方法,像这样:

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

希望能帮助遇到同样问题的其他人。