Loading src/Shopify/lib/AbstractShopify.php +62 −0 Original line number Original line Diff line number Diff line Loading @@ -257,6 +257,19 @@ abstract class AbstractShopify return $this->processResponse($response, $dataKey); return $this->processResponse($response, $dataKey); } } /** * 分页 * @param null $url * @return ['data' => [数据], 'next_link' => '下一页链接'] * @throws Exception */ public function page($url = null) { if (!$url) $url = $this->generateUrl(); $response = HttpRequestJson::get($url, $this->httpHeaders); return $this->processPageResponse($response, $this->pluralizeKey); } /** /** * 根据id获取一条数据 * 根据id获取一条数据 * @param $id * @param $id Loading Loading @@ -380,4 +393,53 @@ abstract class AbstractShopify } } } } /** * 处理响应 * @param array $response * @param null $dataKey * @return mixed * @throws Exception */ public function processPageResponse($response, $dataKey = null) { [$code, $header, $body] = $response; $content = json_decode($body, true); $link = $this->getLink($header); if (isset($content['errors'])) { throw new Exception($this->castString($content['errors']), $code); } if ($dataKey && isset($content[$dataKey])) { $data = $content[$dataKey]; } else { $data = $content; } return ['data' => $data, 'next_link' => $link]; } public function getLink($header, $type = 'next') { if (array_key_exists('x-shopify-api-version', $header) && $header['x-shopify-api-version'] < '2019-07') { return null; } if (!empty($header['link'])) { if (stristr($header['link'], '; rel="' . $type . '"') > -1) { $headerLinks = explode(',', $header['link']); foreach ($headerLinks as $headerLink) { if (stristr($headerLink, '; rel="' . $type . '"') === -1) { continue; } $pattern = '#<(.*?)>; rel="' . $type . '"#m'; preg_match($pattern, $headerLink, $linkResponseHeaders); if ($linkResponseHeaders) { return $linkResponseHeaders[1]; } } } } return null; } } } Loading
src/Shopify/lib/AbstractShopify.php +62 −0 Original line number Original line Diff line number Diff line Loading @@ -257,6 +257,19 @@ abstract class AbstractShopify return $this->processResponse($response, $dataKey); return $this->processResponse($response, $dataKey); } } /** * 分页 * @param null $url * @return ['data' => [数据], 'next_link' => '下一页链接'] * @throws Exception */ public function page($url = null) { if (!$url) $url = $this->generateUrl(); $response = HttpRequestJson::get($url, $this->httpHeaders); return $this->processPageResponse($response, $this->pluralizeKey); } /** /** * 根据id获取一条数据 * 根据id获取一条数据 * @param $id * @param $id Loading Loading @@ -380,4 +393,53 @@ abstract class AbstractShopify } } } } /** * 处理响应 * @param array $response * @param null $dataKey * @return mixed * @throws Exception */ public function processPageResponse($response, $dataKey = null) { [$code, $header, $body] = $response; $content = json_decode($body, true); $link = $this->getLink($header); if (isset($content['errors'])) { throw new Exception($this->castString($content['errors']), $code); } if ($dataKey && isset($content[$dataKey])) { $data = $content[$dataKey]; } else { $data = $content; } return ['data' => $data, 'next_link' => $link]; } public function getLink($header, $type = 'next') { if (array_key_exists('x-shopify-api-version', $header) && $header['x-shopify-api-version'] < '2019-07') { return null; } if (!empty($header['link'])) { if (stristr($header['link'], '; rel="' . $type . '"') > -1) { $headerLinks = explode(',', $header['link']); foreach ($headerLinks as $headerLink) { if (stristr($headerLink, '; rel="' . $type . '"') === -1) { continue; } $pattern = '#<(.*?)>; rel="' . $type . '"#m'; preg_match($pattern, $headerLink, $linkResponseHeaders); if ($linkResponseHeaders) { return $linkResponseHeaders[1]; } } } } return null; } } }