Commit b4fc60a5 authored by 王源's avatar 王源 🎧

Merge branch 'shopify' into test

parents fdf13407 c07263e0
......@@ -257,6 +257,19 @@ abstract class AbstractShopify
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获取一条数据
* @param $id
......@@ -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;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment