简体   繁体   中英

How do I convert this mailchimp API request using Curl to Guzzle with PHP?

I have a curl request that works with the mailchimp API but I wanted to try using Guzzle. I can not get the request to work with Guzzle.

What I want is to have the API response converted to a php array I can manipulate.

I've tried the following code to convert curl to guzzle but it's not working.

This code works fine


    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);
    }

When I try this guzzle code I don't see the result of the request but I don't get errors either.


    $client = new GuzzleHttp\Client();
    $json = $client->request('GET', $url, [
    'headers' => [
        'Authorization' => 'apikey ' . $api_key,
        'Accept' => 'application/json',
        'Content-type' => 'application/json'
    ]]);

Expected result is an array that I can manipulate with data like this:

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
)

The actual results is this:

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 ( ) ) )

Using guzzle, your $client->request returns a Response object. To get the output as array, decode the json string with true as the second param.

$response = $client->request('GET', $url, [ ... ]]);
$json_array = json_decode($response->getBody(), true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM