如何使用Curl将这个mailchimp API请求转换为使用PHP的Guzzle?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用Curl将这个mailchimp API请求转换为使用PHP的Guzzle?相关的知识,希望对你有一定的参考价值。
我有一个与mailchimp API一起使用的curl请求,但我想尝试使用Guzzle。我无法得到与Guzzle合作的请求。
我想要的是将API响应转换为我可以操作的php数组。
我已经尝试了以下代码将curl转换为guzzle,但它不起作用。
这段代码工作正常
function apiRequest($url,$post = NULL) {
global $api_key;
$httpHeader = array(
'Accept: application/vnd.api+json',
'Content-Type: application/vnd.api+json',
'Authorization: apikey ' . $api_key
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if(isset($post)) curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$responseContent = curl_exec($ch);
$response['headers'] = curl_getinfo($ch);
curl_close($ch);
return json_decode($responseContent,true);
}
当我尝试这个guzzle代码时,我没有看到请求的结果,但我也没有得到错误。
$client = new GuzzleHttp\Client();
$json = $client->request('GET', $url, [
'headers' => [
'Authorization' => 'apikey ' . $api_key,
'Accept' => 'application/json',
'Content-type' => 'application/json'
]]);
预期的结果是一个我可以用这样的数据操作的数组:
Array
(
[id] => 14721
[name] => 2019_3_test
[member_count] => 1
[type] => static
[created_at] => 2019-03-23T00:55:02+00:00
[updated_at] => 2019-03-23T00:55:02+00:00
[list_id] => f4f3d03d5f
)
实际结果如下:
GuzzleHttp\Psr7\Response Object ( [reasonPhrase:GuzzleHttp\Psr7\Response:private] => OK [statusCode:GuzzleHttp\Psr7\Response:private] => 200 [headers:GuzzleHttp\Psr7\Response:private] => Array ( [Server] => Array ( [0] => openresty ) [Content-Type] => Array ( [0] => application/json; charset=utf-8 ) [Content-Length] => Array ( [0] => 1326 ) [Vary] => Array ( [0] => Accept-Encoding ) [X-Request-Id] => Array ( [0] => 342c0c4b-5629-4bc4-aa7d-4a7f6d51af7b ) [Link] => Array ( [0] => ; rel="describedBy" ) [Date] => Array ( [0] => Sat, 23 Mar 2019 04:43:57 GMT ) [Connection] => Array ( [0] => keep-alive ) [Set-Cookie] => Array ( [0] => _AVESTA_ENVIRONMENT=prod; path=/ [1] => _mcid=1.746c6e8ecb1ba60edb191ed80a3c86c1; expires=Sun, 22-Mar-2020 04:43:57 GMT; Max-Age=31536000; path=/; domain=.mailchimp.com ) ) [headerNames:GuzzleHttp\Psr7\Response:private] => Array ( [server] => Server [content-type] => Content-Type [content-length] => Content-Length [vary] => Vary [x-request-id] => X-Request-Id [link] => Link [date] => Date [connection] => Connection [set-cookie] => Set-Cookie ) [protocol:GuzzleHttp\Psr7\Response:private] => 1.1 [stream:GuzzleHttp\Psr7\Response:private] => GuzzleHttp\Psr7\Stream Object ( [stream:GuzzleHttp\Psr7\Stream:private] => Resource id #40 [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 ( ) ) )
答案
使用guzzle,你的$client->request
返回一个Response
对象。要将输出作为数组,请使用true作为第二个参数解码json字符串。
$response = $client->request('GET', $url, [ ... ]]);
$json = json_decode($response->getBody(), true);
以上是关于如何使用Curl将这个mailchimp API请求转换为使用PHP的Guzzle?的主要内容,如果未能解决你的问题,请参考以下文章
无法让 MailChimp 使用带有 Curl 的 API 保存我的数据
通过 Curl (Mailchimp API) 发布 PHP 会话
使用 cURL 和 Mailchimp API v3 更新列表中的订阅者
Mailchimp v3.0 API,使用 Perl Curl