Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
meibuyu-micro
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
without authentication
meibuyu-micro
Commits
914ea9bf
Commit
914ea9bf
authored
Sep 05, 2020
by
王源
🎧
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
调整shopify请求底层为curl模式
parent
fc3adf30
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
368 additions
and
41 deletions
+368
-41
AbstractShopify.php
src/Shopify/lib/AbstractShopify.php
+115
-19
CurlRequest.php
src/Tools/CurlRequest.php
+97
-0
CurlResponse.php
src/Tools/CurlResponse.php
+75
-0
HttpRequestJson.php
src/Tools/HttpRequestJson.php
+81
-22
No files found.
src/Shopify/lib/AbstractShopify.php
View file @
914ea9bf
...
...
@@ -153,6 +153,95 @@ abstract class AbstractShopify
return
$resourceUrl
.
(
$customAction
?
"/
$customAction
"
:
''
)
.
'.json'
.
(
!
empty
(
$urlParams
)
?
'?'
.
http_build_query
(
$urlParams
)
:
''
);
}
// /**
// * @param array $urlParams
// * @param null $url
// * @param null $dataKey
// * @return mixed
// * @throws Exception
// */
// public function get($urlParams = [], $url = null, $dataKey = null)
// {
// if (!$url) $url = $this->generateUrl($urlParams);
// $response = HttpRequestJson::get($url, $this->httpHeaders);
// if (!$dataKey) $dataKey = $this->id ? $this->resourceKey : $this->pluralizeKey;
// return $this->processResponse($response, $dataKey);
// }
//
// /**
// * 根据id获取一条数据
// * @param $id
// * @param array $urlParams
// * @return mixed
// * @throws Exception
// */
// public function show($id, $urlParams = [])
// {
// $url = $this->generateUrl($urlParams, $id);
// $response = HttpRequestJson::get($url, $this->httpHeaders);
// return $this->processResponse($response, $this->resourceKey);
// }
//
// /**
// * 获取数量
// * @param array $urlParams
// * @return mixed
// * @throws Exception
// */
// public function count($urlParams = [])
// {
// if (!$this->countEnabled) {
// throw new Exception("当前类{$this->getResourceName()}不支持count()方法");
// }
// $url = $this->generateUrl($urlParams, null, 'count');
// return $this->get([], $url, 'count');
// }
//
// /**
// * @param $dataArray
// * @param null $url
// * @param bool $wrapData
// * @return mixed
// * @throws Exception
// */
// public function post($dataArray, $url = null, $wrapData = true)
// {
// if (!$url) $url = $this->generateUrl();
// if ($wrapData && !empty($dataArray)) $dataArray = $this->wrapData($dataArray);
// $response = HttpRequestJson::post($url, $dataArray, $this->httpHeaders);
// return $this->processResponse($response, $this->resourceKey);
// }
//
// /**
// * @param int|string $id
// * @param $dataArray
// * @param null $url
// * @param bool $wrapData
// * @return mixed
// * @throws Exception
// */
// public function put($id, $dataArray, $url = null, $wrapData = true)
// {
// if (!$url) $url = $this->generateUrl([], $id);
// if ($wrapData && !empty($dataArray)) $dataArray = $this->wrapData($dataArray);
// $response = HttpRequestJson::put($url, $dataArray, $this->httpHeaders);
// return $this->processResponse($response, $this->resourceKey);
// }
//
// /**
// * @param int|string $id
// * @param array $urlParams
// * @param null $url
// * @return mixed
// * @throws Exception
// */
// public function delete($id = null, $urlParams = [], $url = null)
// {
// if (!$url) $url = $this->generateUrl($urlParams, $id);
// $response = HttpRequestJson::delete($url, $this->httpHeaders);
// return $this->processResponse($response);
// }
/**
* @param array $urlParams
* @param null $url
...
...
@@ -182,21 +271,6 @@ abstract class AbstractShopify
return
$this
->
processResponse
(
$response
,
$this
->
resourceKey
);
}
/**
* 获取数量
* @param array $urlParams
* @return mixed
* @throws Exception
*/
public
function
count
(
$urlParams
=
[])
{
if
(
!
$this
->
countEnabled
)
{
throw
new
Exception
(
"当前类
{
$this
->
getResourceName
()
}
不支持count()方法"
);
}
$url
=
$this
->
generateUrl
(
$urlParams
,
null
,
'count'
);
return
$this
->
get
([],
$url
,
'count'
);
}
/**
* @param $dataArray
* @param null $url
...
...
@@ -268,14 +342,36 @@ abstract class AbstractShopify
* @return mixed
* @throws Exception
*/
// public function processResponse($response, $dataKey = null)
// {
// $httpCode = $response->getStatusCode();
// $content = $response->getBody()->getContents();
// $content = json_decode($content, true);
//
// if (isset($content['errors'])) {
// throw new Exception($this->castString($content['errors']), $httpCode);
// }
// if ($dataKey && isset($content[$dataKey])) {
// return $content[$dataKey];
// } else {
// return $content;
// }
// }
/**
* 处理响应
* @param array $response
* @param null $dataKey
* @return mixed
* @throws Exception
*/
public
function
processResponse
(
$response
,
$dataKey
=
null
)
{
$httpCode
=
$response
->
getStatusCode
();
$content
=
$response
->
getBody
()
->
getContents
();
$content
=
json_decode
(
$content
,
true
);
[
$code
,
$header
,
$body
]
=
$response
;
$content
=
json_decode
(
$body
,
true
);
if
(
isset
(
$content
[
'errors'
]))
{
throw
new
Exception
(
$this
->
castString
(
$content
[
'errors'
]),
$
httpC
ode
);
throw
new
Exception
(
$this
->
castString
(
$content
[
'errors'
]),
$
c
ode
);
}
if
(
$dataKey
&&
isset
(
$content
[
$dataKey
]))
{
return
$content
[
$dataKey
];
...
...
src/Tools/CurlRequest.php
0 → 100644
View file @
914ea9bf
<?php
namespace
Meibuyu\Micro\Tools
;
use
Exception
;
class
CurlRequest
{
protected
static
function
init
(
$url
,
$httpHeaders
=
[])
{
// Create Curl resource
$ch
=
curl_init
();
// Set URL
curl_setopt
(
$ch
,
CURLOPT_URL
,
$url
);
//Return the transfer as a string
curl_setopt
(
$ch
,
CURLOPT_RETURNTRANSFER
,
1
);
curl_setopt
(
$ch
,
CURLOPT_HEADER
,
true
);
curl_setopt
(
$ch
,
CURLOPT_USERAGENT
,
'PHPClassic/PHPShopify'
);
$headers
=
[];
foreach
(
$httpHeaders
as
$key
=>
$value
)
{
$headers
[]
=
"
$key
:
$value
"
;
}
//Set HTTP Headers
curl_setopt
(
$ch
,
CURLOPT_HTTPHEADER
,
$headers
);
return
$ch
;
}
public
static
function
get
(
$url
,
$httpHeaders
=
[])
{
$ch
=
self
::
init
(
$url
,
$httpHeaders
);
return
self
::
processRequest
(
$ch
);
}
public
static
function
post
(
$url
,
$data
,
$httpHeaders
=
[])
{
$ch
=
self
::
init
(
$url
,
$httpHeaders
);
curl_setopt
(
$ch
,
CURLOPT_CUSTOMREQUEST
,
'POST'
);
curl_setopt
(
$ch
,
CURLOPT_POSTFIELDS
,
$data
);
return
self
::
processRequest
(
$ch
);
}
public
static
function
put
(
$url
,
$data
,
$httpHeaders
=
[])
{
$ch
=
self
::
init
(
$url
,
$httpHeaders
);
curl_setopt
(
$ch
,
CURLOPT_CUSTOMREQUEST
,
'PUT'
);
curl_setopt
(
$ch
,
CURLOPT_POSTFIELDS
,
$data
);
return
self
::
processRequest
(
$ch
);
}
public
static
function
delete
(
$url
,
$httpHeaders
=
[])
{
$ch
=
self
::
init
(
$url
,
$httpHeaders
);
curl_setopt
(
$ch
,
CURLOPT_CUSTOMREQUEST
,
'DELETE'
);
return
self
::
processRequest
(
$ch
);
}
protected
static
function
processRequest
(
$ch
)
{
# Check for 429 leaky bucket error
while
(
1
)
{
$output
=
curl_exec
(
$ch
);
$response
=
new
CurlResponse
(
$output
);
$httpCode
=
curl_getinfo
(
$ch
,
CURLINFO_HTTP_CODE
);
if
(
$httpCode
!=
429
)
{
break
;
}
$limitHeader
=
explode
(
'/'
,
$response
->
getHeader
(
'X-Shopify-Shop-Api-Call-Limit'
),
2
);
if
(
isset
(
$limitHeader
[
1
])
&&
$limitHeader
[
0
]
<
$limitHeader
[
1
])
{
throw
new
Exception
(
$response
->
getBody
());
}
usleep
(
500000
);
}
if
(
curl_errno
(
$ch
))
{
throw
new
Exception
(
curl_errno
(
$ch
)
.
' : '
.
curl_error
(
$ch
));
}
// close curl resource to free up system resources
curl_close
(
$ch
);
$httpHeader
=
$response
->
getHeaders
();
return
[
$httpCode
,
$httpHeader
,
$response
->
getBody
()];
}
}
src/Tools/CurlResponse.php
0 → 100644
View file @
914ea9bf
<?php
namespace
Meibuyu\Micro\Tools
;
class
CurlResponse
{
/** @var array */
private
$headers
=
[];
/** @var string */
private
$body
;
public
function
__construct
(
$response
)
{
$this
->
parse
(
$response
);
}
/**
* @param string $response
*/
private
function
parse
(
$response
)
{
$response
=
\explode
(
"
\r\n\r\n
"
,
$response
);
if
(
\count
(
$response
)
>
1
)
{
// We want the last two parts
$response
=
\array_slice
(
$response
,
-
2
,
2
);
list
(
$headers
,
$body
)
=
$response
;
foreach
(
\explode
(
"
\r\n
"
,
$headers
)
as
$header
)
{
$pair
=
\explode
(
': '
,
$header
,
2
);
if
(
isset
(
$pair
[
1
]))
{
$headerKey
=
strtolower
(
$pair
[
0
]);
$this
->
headers
[
$headerKey
]
=
$pair
[
1
];
}
}
}
else
{
$body
=
$response
[
0
];
}
$this
->
body
=
$body
;
}
/**
* @return array
*/
public
function
getHeaders
()
{
return
$this
->
headers
;
}
/**
* @param string $key
*
* @return string
*/
public
function
getHeader
(
$key
)
{
return
isset
(
$this
->
headers
[
$key
])
?
$this
->
headers
[
$key
]
:
null
;
}
/**
* @return string
*/
public
function
getBody
()
{
return
$this
->
body
;
}
public
function
__toString
()
{
$body
=
$this
->
getBody
();
$body
=
$body
?:
''
;
return
$body
;
}
}
src/Tools/HttpRequestJson.php
View file @
914ea9bf
...
...
@@ -8,8 +8,8 @@
namespace
Meibuyu\Micro\Tools
;
use
GuzzleHttp\Client
;
use
Psr\Http\Message\ResponseInterface
;
//
use GuzzleHttp\Client;
//
use Psr\Http\Message\ResponseInterface;
/**
* json格式请求(非协程)
...
...
@@ -19,54 +19,113 @@ use Psr\Http\Message\ResponseInterface;
class
HttpRequestJson
{
protected
static
function
prepareRequest
(
&
$headers
=
[],
&
$data
=
[])
{
$data
=
json_encode
(
$data
);
if
(
!
$data
)
{
$headers
[
'Content-type'
]
=
'application/json'
;
$headers
[
'Content-Length'
]
=
strlen
(
$data
);
}
}
// /**
// * get请求
// * @param $url
// * @param array $httpHeaders
// * @return ResponseInterface
// */
// public static function get($url, $httpHeaders = [])
// {
// $client = new Client(['timeout' => 30]);
// return $client->get($url, ['headers' => $httpHeaders]);
// }
//
// /**
// * post请求
// * @param $url
// * @param $dataArray
// * @param array $httpHeaders
// * @return ResponseInterface
// */
// public static function post($url, $dataArray, $httpHeaders = [])
// {
// $client = new Client(['timeout' => 30]);
// return $client->post($url, ['headers' => $httpHeaders, 'json' => $dataArray]);
// }
//
// /**
// * put请求
// * @param $url
// * @param $dataArray
// * @param array $httpHeaders
// * @return ResponseInterface
// */
// public static function put($url, $dataArray, $httpHeaders = [])
// {
// $client = new Client(['timeout' => 30]);
// return $client->put($url, ['headers' => $httpHeaders, 'json' => $dataArray]);
// }
//
// /**
// * delete请求
// * @param $url
// * @param array $httpHeaders
// * @return ResponseInterface
// */
// public static function delete($url, $httpHeaders = [])
// {
// $client = new Client(['timeout' => 30]);
// return $client->delete($url, ['headers' => $httpHeaders]);
// }
/**
* get请求
* @param $url
* @param array $h
ttpH
eaders
* @return
ResponseInterface
* @param array $headers
* @return
array
*/
public
static
function
get
(
$url
,
$h
ttpH
eaders
=
[])
public
static
function
get
(
$url
,
$headers
=
[])
{
$client
=
new
Client
([
'timeout'
=>
30
]
);
return
$client
->
get
(
$url
,
[
'headers'
=>
$httpHeaders
]
);
self
::
prepareRequest
(
$headers
);
return
CurlRequest
::
get
(
$url
,
$headers
);
}
/**
* post请求
* @param $url
* @param $dataArray
* @param array $h
ttpH
eaders
* @return
ResponseInterface
* @param array $headers
* @return
array
*/
public
static
function
post
(
$url
,
$dataArray
,
$h
ttpH
eaders
=
[])
public
static
function
post
(
$url
,
$dataArray
,
$headers
=
[])
{
$client
=
new
Client
([
'timeout'
=>
30
]
);
return
$client
->
post
(
$url
,
[
'headers'
=>
$httpHeaders
,
'json'
=>
$dataArray
]
);
self
::
prepareRequest
(
$headers
,
$dataArray
);
return
CurlRequest
::
post
(
$url
,
$dataArray
,
$headers
);
}
/**
* put请求
* @param $url
* @param $dataArray
* @param array $h
ttpH
eaders
* @return
ResponseInterface
* @param array $headers
* @return
array
*/
public
static
function
put
(
$url
,
$dataArray
,
$h
ttpH
eaders
=
[])
public
static
function
put
(
$url
,
$dataArray
,
$headers
=
[])
{
$client
=
new
Client
([
'timeout'
=>
30
]
);
return
$client
->
put
(
$url
,
[
'headers'
=>
$httpHeaders
,
'json'
=>
$dataArray
]
);
self
::
prepareRequest
(
$headers
,
$dataArray
);
return
CurlRequest
::
put
(
$url
,
$dataArray
,
$headers
);
}
/**
* delete请求
* @param $url
* @param array $h
ttpH
eaders
* @return
ResponseInterface
* @param array $headers
* @return
array
*/
public
static
function
delete
(
$url
,
$h
ttpH
eaders
=
[])
public
static
function
delete
(
$url
,
$headers
=
[])
{
$client
=
new
Client
([
'timeout'
=>
30
]
);
return
$client
->
delete
(
$url
,
[
'headers'
=>
$httpHeaders
]
);
self
::
prepareRequest
(
$headers
);
return
CurlRequest
::
delete
(
$url
,
$headers
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment