How do I convert this mailchimp API request using Curl to Guzzle with PHP?How can I prevent SQL injection in PHP?Convert HTML + CSS to PDF with PHP?How do I make a redirect in PHP?How do I get PHP errors to display?How do I get a YouTube video thumbnail from the YouTube API?How do you parse and process HTML/XML in PHP?How does PHP 'foreach' actually work?Guzzle 6 send multipart dataunable to send json as guzzle post bodyConvert Postman request to guzzle or other PHP HTTP client

Emergency stop in plain TeX, pdfTeX, XeTeX and LuaTeX?

What are the requirements for a river delta to form?

Explaining intravenous drug abuse to a small child

How would you say "You forget wearing what you're wearing"?

Primes in a Diamond

How did the Force make Luke hard to hit in the Battle of Yavin?

Can a player choose to add detail and flavor to their character's spells and abilities?

Why doesn't a particle exert force on itself?

Problem with estimating a sequence with intuition

Is it normal for gliders not to have attitude indicators?

Append unique characters read from filecontents to a string

Determine if a grid contains another grid

How long does it take a postcard to get from USA to Germany?

Ab major 9th chord in Bach

GitLab account hacked and repo wiped

How long did it take Captain Marvel to travel to Earth?

Huffman Code in C++

What word describes the sound of an instrument based on the shape of the waveform of its sound?

Python 3 - simple temperature program version 1.3

How to use awk to extract data from a file based on the content of another file?

Is crescere the correct word meaning to to grow or cultivate?

Installing Debian 10, upgrade to stable later?

Collision domain question

Hostile Divisor Numbers



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


How can I prevent SQL injection in PHP?Convert HTML + CSS to PDF with PHP?How do I make a redirect in PHP?How do I get PHP errors to display?How do I get a YouTube video thumbnail from the YouTube API?How do you parse and process HTML/XML in PHP?How does PHP 'foreach' actually work?Guzzle 6 send multipart dataunable to send json as guzzle post bodyConvert Postman request to guzzle or other PHP HTTP client






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















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 GuzzleHttpClient();
$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:



GuzzleHttpPsr7Response Object ( [reasonPhrase:GuzzleHttpPsr7Response:private] => OK [statusCode:GuzzleHttpPsr7Response:private] => 200 [headers:GuzzleHttpPsr7Response: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:GuzzleHttpPsr7Response: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:GuzzleHttpPsr7Response:private] => 1.1 [stream:GuzzleHttpPsr7Response:private] => GuzzleHttpPsr7Stream Object ( [stream:GuzzleHttpPsr7Stream:private] => Resource id #40 [size:GuzzleHttpPsr7Stream:private] => [seekable:GuzzleHttpPsr7Stream:private] => 1 [readable:GuzzleHttpPsr7Stream:private] => 1 [writable:GuzzleHttpPsr7Stream:private] => 1 [uri:GuzzleHttpPsr7Stream:private] => php://temp [customMetadata:GuzzleHttpPsr7Stream:private] => Array ( ) ) )









share|improve this question




























    0















    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 GuzzleHttpClient();
    $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:



    GuzzleHttpPsr7Response Object ( [reasonPhrase:GuzzleHttpPsr7Response:private] => OK [statusCode:GuzzleHttpPsr7Response:private] => 200 [headers:GuzzleHttpPsr7Response: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:GuzzleHttpPsr7Response: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:GuzzleHttpPsr7Response:private] => 1.1 [stream:GuzzleHttpPsr7Response:private] => GuzzleHttpPsr7Stream Object ( [stream:GuzzleHttpPsr7Stream:private] => Resource id #40 [size:GuzzleHttpPsr7Stream:private] => [seekable:GuzzleHttpPsr7Stream:private] => 1 [readable:GuzzleHttpPsr7Stream:private] => 1 [writable:GuzzleHttpPsr7Stream:private] => 1 [uri:GuzzleHttpPsr7Stream:private] => php://temp [customMetadata:GuzzleHttpPsr7Stream:private] => Array ( ) ) )









    share|improve this question
























      0












      0








      0








      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 GuzzleHttpClient();
      $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:



      GuzzleHttpPsr7Response Object ( [reasonPhrase:GuzzleHttpPsr7Response:private] => OK [statusCode:GuzzleHttpPsr7Response:private] => 200 [headers:GuzzleHttpPsr7Response: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:GuzzleHttpPsr7Response: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:GuzzleHttpPsr7Response:private] => 1.1 [stream:GuzzleHttpPsr7Response:private] => GuzzleHttpPsr7Stream Object ( [stream:GuzzleHttpPsr7Stream:private] => Resource id #40 [size:GuzzleHttpPsr7Stream:private] => [seekable:GuzzleHttpPsr7Stream:private] => 1 [readable:GuzzleHttpPsr7Stream:private] => 1 [writable:GuzzleHttpPsr7Stream:private] => 1 [uri:GuzzleHttpPsr7Stream:private] => php://temp [customMetadata:GuzzleHttpPsr7Stream:private] => Array ( ) ) )









      share|improve this question














      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 GuzzleHttpClient();
      $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:



      GuzzleHttpPsr7Response Object ( [reasonPhrase:GuzzleHttpPsr7Response:private] => OK [statusCode:GuzzleHttpPsr7Response:private] => 200 [headers:GuzzleHttpPsr7Response: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:GuzzleHttpPsr7Response: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:GuzzleHttpPsr7Response:private] => 1.1 [stream:GuzzleHttpPsr7Response:private] => GuzzleHttpPsr7Stream Object ( [stream:GuzzleHttpPsr7Stream:private] => Resource id #40 [size:GuzzleHttpPsr7Stream:private] => [seekable:GuzzleHttpPsr7Stream:private] => 1 [readable:GuzzleHttpPsr7Stream:private] => 1 [writable:GuzzleHttpPsr7Stream:private] => 1 [uri:GuzzleHttpPsr7Stream:private] => php://temp [customMetadata:GuzzleHttpPsr7Stream:private] => Array ( ) ) )






      php api guzzle mailchimp-api-v3.0






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 23 at 4:48









      bkpbkp

      31




      31






















          1 Answer
          1






          active

          oldest

          votes


















          0














          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 = json_decode($response->getBody(), true);





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55310698%2fhow-do-i-convert-this-mailchimp-api-request-using-curl-to-guzzle-with-php%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            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 = json_decode($response->getBody(), true);





            share|improve this answer



























              0














              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 = json_decode($response->getBody(), true);





              share|improve this answer

























                0












                0








                0







                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 = json_decode($response->getBody(), true);





                share|improve this answer













                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 = json_decode($response->getBody(), true);






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 23 at 5:00









                NMCNMC

                697816




                697816





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55310698%2fhow-do-i-convert-this-mailchimp-api-request-using-curl-to-guzzle-with-php%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript