Call with curl works, its version with Guzzle 6 does not












0















I am trying to convert a code that works, to its version with Guzzle, but I do not get the desired result, and I think it is due to a lack of understanding of Guzzle v6.



If I execute the following code, it's work fine



$postfields = array(
'identifier' => $this->username,
'secret' => $this->password,
'accesskey' => $this->accesskey,
'action' => 'GetClients',
'responsetype' => self::RESPONSE_TYPE,
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url.self::ENDPOINT);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields));

$response = curl_exec($ch);

if (curl_error($ch)) {
die('Unable to connect: ' . curl_errno($ch) . ' - ' . curl_error($ch));
}

curl_close($ch);

// Decode response
$jsonData = json_decode($response, true);


But if I try to convert to Guzzle v6, fails



$client = new GuzzleHttpClient();

$response = $client->post($this->url.self::ENDPOINT, [
'headers' => [
'action' => 'GetClients',
'identifier' => $this->username,
'secret' => $this->password,
'accesskey' => $this->accesskey,
'responsetype' => self::RESPONSE_TYPE
]
]);


I get an exception



GuzzleHttpExceptionClientException  : Client error: `GET https://mydomain.test/intranet/includes/api.php` resulted in a `403 Forbidden` response:
result=error;message=Invalid IP 2.137.XXX.XX


On the other hand I do not know how to understand the call (url that forms Guzzle) with the get() method of Guzzle client.










share|improve this question




















  • 1





    Your two calls are doing completely different things - a POST request with the parameters in the request body, and a GET request with them as headers

    – iainn
    Nov 14 '18 at 17:35











  • Oh my good... I don't see POST method.... A lot of thanks.

    – abkrim
    Nov 14 '18 at 17:43











  • also with -post() method not work. Same result or exception. I've edited code

    – abkrim
    Nov 14 '18 at 18:08











  • Why are you posting a multi-dimensional array?

    – miken32
    Nov 14 '18 at 18:12











  • @miken32 If not use a array, how to send compose url with guzzle in post method?

    – abkrim
    Nov 14 '18 at 18:17


















0















I am trying to convert a code that works, to its version with Guzzle, but I do not get the desired result, and I think it is due to a lack of understanding of Guzzle v6.



If I execute the following code, it's work fine



$postfields = array(
'identifier' => $this->username,
'secret' => $this->password,
'accesskey' => $this->accesskey,
'action' => 'GetClients',
'responsetype' => self::RESPONSE_TYPE,
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url.self::ENDPOINT);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields));

$response = curl_exec($ch);

if (curl_error($ch)) {
die('Unable to connect: ' . curl_errno($ch) . ' - ' . curl_error($ch));
}

curl_close($ch);

// Decode response
$jsonData = json_decode($response, true);


But if I try to convert to Guzzle v6, fails



$client = new GuzzleHttpClient();

$response = $client->post($this->url.self::ENDPOINT, [
'headers' => [
'action' => 'GetClients',
'identifier' => $this->username,
'secret' => $this->password,
'accesskey' => $this->accesskey,
'responsetype' => self::RESPONSE_TYPE
]
]);


I get an exception



GuzzleHttpExceptionClientException  : Client error: `GET https://mydomain.test/intranet/includes/api.php` resulted in a `403 Forbidden` response:
result=error;message=Invalid IP 2.137.XXX.XX


On the other hand I do not know how to understand the call (url that forms Guzzle) with the get() method of Guzzle client.










share|improve this question




















  • 1





    Your two calls are doing completely different things - a POST request with the parameters in the request body, and a GET request with them as headers

    – iainn
    Nov 14 '18 at 17:35











  • Oh my good... I don't see POST method.... A lot of thanks.

    – abkrim
    Nov 14 '18 at 17:43











  • also with -post() method not work. Same result or exception. I've edited code

    – abkrim
    Nov 14 '18 at 18:08











  • Why are you posting a multi-dimensional array?

    – miken32
    Nov 14 '18 at 18:12











  • @miken32 If not use a array, how to send compose url with guzzle in post method?

    – abkrim
    Nov 14 '18 at 18:17
















0












0








0








I am trying to convert a code that works, to its version with Guzzle, but I do not get the desired result, and I think it is due to a lack of understanding of Guzzle v6.



If I execute the following code, it's work fine



$postfields = array(
'identifier' => $this->username,
'secret' => $this->password,
'accesskey' => $this->accesskey,
'action' => 'GetClients',
'responsetype' => self::RESPONSE_TYPE,
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url.self::ENDPOINT);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields));

$response = curl_exec($ch);

if (curl_error($ch)) {
die('Unable to connect: ' . curl_errno($ch) . ' - ' . curl_error($ch));
}

curl_close($ch);

// Decode response
$jsonData = json_decode($response, true);


But if I try to convert to Guzzle v6, fails



$client = new GuzzleHttpClient();

$response = $client->post($this->url.self::ENDPOINT, [
'headers' => [
'action' => 'GetClients',
'identifier' => $this->username,
'secret' => $this->password,
'accesskey' => $this->accesskey,
'responsetype' => self::RESPONSE_TYPE
]
]);


I get an exception



GuzzleHttpExceptionClientException  : Client error: `GET https://mydomain.test/intranet/includes/api.php` resulted in a `403 Forbidden` response:
result=error;message=Invalid IP 2.137.XXX.XX


On the other hand I do not know how to understand the call (url that forms Guzzle) with the get() method of Guzzle client.










share|improve this question
















I am trying to convert a code that works, to its version with Guzzle, but I do not get the desired result, and I think it is due to a lack of understanding of Guzzle v6.



If I execute the following code, it's work fine



$postfields = array(
'identifier' => $this->username,
'secret' => $this->password,
'accesskey' => $this->accesskey,
'action' => 'GetClients',
'responsetype' => self::RESPONSE_TYPE,
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url.self::ENDPOINT);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields));

$response = curl_exec($ch);

if (curl_error($ch)) {
die('Unable to connect: ' . curl_errno($ch) . ' - ' . curl_error($ch));
}

curl_close($ch);

// Decode response
$jsonData = json_decode($response, true);


But if I try to convert to Guzzle v6, fails



$client = new GuzzleHttpClient();

$response = $client->post($this->url.self::ENDPOINT, [
'headers' => [
'action' => 'GetClients',
'identifier' => $this->username,
'secret' => $this->password,
'accesskey' => $this->accesskey,
'responsetype' => self::RESPONSE_TYPE
]
]);


I get an exception



GuzzleHttpExceptionClientException  : Client error: `GET https://mydomain.test/intranet/includes/api.php` resulted in a `403 Forbidden` response:
result=error;message=Invalid IP 2.137.XXX.XX


On the other hand I do not know how to understand the call (url that forms Guzzle) with the get() method of Guzzle client.







php guzzle guzzle6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 18:07







abkrim

















asked Nov 14 '18 at 17:25









abkrimabkrim

1,45022337




1,45022337








  • 1





    Your two calls are doing completely different things - a POST request with the parameters in the request body, and a GET request with them as headers

    – iainn
    Nov 14 '18 at 17:35











  • Oh my good... I don't see POST method.... A lot of thanks.

    – abkrim
    Nov 14 '18 at 17:43











  • also with -post() method not work. Same result or exception. I've edited code

    – abkrim
    Nov 14 '18 at 18:08











  • Why are you posting a multi-dimensional array?

    – miken32
    Nov 14 '18 at 18:12











  • @miken32 If not use a array, how to send compose url with guzzle in post method?

    – abkrim
    Nov 14 '18 at 18:17
















  • 1





    Your two calls are doing completely different things - a POST request with the parameters in the request body, and a GET request with them as headers

    – iainn
    Nov 14 '18 at 17:35











  • Oh my good... I don't see POST method.... A lot of thanks.

    – abkrim
    Nov 14 '18 at 17:43











  • also with -post() method not work. Same result or exception. I've edited code

    – abkrim
    Nov 14 '18 at 18:08











  • Why are you posting a multi-dimensional array?

    – miken32
    Nov 14 '18 at 18:12











  • @miken32 If not use a array, how to send compose url with guzzle in post method?

    – abkrim
    Nov 14 '18 at 18:17










1




1





Your two calls are doing completely different things - a POST request with the parameters in the request body, and a GET request with them as headers

– iainn
Nov 14 '18 at 17:35





Your two calls are doing completely different things - a POST request with the parameters in the request body, and a GET request with them as headers

– iainn
Nov 14 '18 at 17:35













Oh my good... I don't see POST method.... A lot of thanks.

– abkrim
Nov 14 '18 at 17:43





Oh my good... I don't see POST method.... A lot of thanks.

– abkrim
Nov 14 '18 at 17:43













also with -post() method not work. Same result or exception. I've edited code

– abkrim
Nov 14 '18 at 18:08





also with -post() method not work. Same result or exception. I've edited code

– abkrim
Nov 14 '18 at 18:08













Why are you posting a multi-dimensional array?

– miken32
Nov 14 '18 at 18:12





Why are you posting a multi-dimensional array?

– miken32
Nov 14 '18 at 18:12













@miken32 If not use a array, how to send compose url with guzzle in post method?

– abkrim
Nov 14 '18 at 18:17







@miken32 If not use a array, how to send compose url with guzzle in post method?

– abkrim
Nov 14 '18 at 18:17














1 Answer
1






active

oldest

votes


















0














An error in understanding the mechanism of sending the parameters, using headers instead of a correct form_params since it is a POST method



Correct code is



$response = $client->post($this->url.self::ENDPOINT, [
'form_params' => [
'action' => 'GetClients',
'identifier' => $this->username,
'secret' => $this->password,
'accesskey' => $this->accesskey,
'responsetype' => self::RESPONSE_TYPE
]
]);





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%2f53305697%2fcall-with-curl-works-its-version-with-guzzle-6-does-not%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














    An error in understanding the mechanism of sending the parameters, using headers instead of a correct form_params since it is a POST method



    Correct code is



    $response = $client->post($this->url.self::ENDPOINT, [
    'form_params' => [
    'action' => 'GetClients',
    'identifier' => $this->username,
    'secret' => $this->password,
    'accesskey' => $this->accesskey,
    'responsetype' => self::RESPONSE_TYPE
    ]
    ]);





    share|improve this answer




























      0














      An error in understanding the mechanism of sending the parameters, using headers instead of a correct form_params since it is a POST method



      Correct code is



      $response = $client->post($this->url.self::ENDPOINT, [
      'form_params' => [
      'action' => 'GetClients',
      'identifier' => $this->username,
      'secret' => $this->password,
      'accesskey' => $this->accesskey,
      'responsetype' => self::RESPONSE_TYPE
      ]
      ]);





      share|improve this answer


























        0












        0








        0







        An error in understanding the mechanism of sending the parameters, using headers instead of a correct form_params since it is a POST method



        Correct code is



        $response = $client->post($this->url.self::ENDPOINT, [
        'form_params' => [
        'action' => 'GetClients',
        'identifier' => $this->username,
        'secret' => $this->password,
        'accesskey' => $this->accesskey,
        'responsetype' => self::RESPONSE_TYPE
        ]
        ]);





        share|improve this answer













        An error in understanding the mechanism of sending the parameters, using headers instead of a correct form_params since it is a POST method



        Correct code is



        $response = $client->post($this->url.self::ENDPOINT, [
        'form_params' => [
        'action' => 'GetClients',
        'identifier' => $this->username,
        'secret' => $this->password,
        'accesskey' => $this->accesskey,
        'responsetype' => self::RESPONSE_TYPE
        ]
        ]);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 18:32









        abkrimabkrim

        1,45022337




        1,45022337
































            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%2f53305697%2fcall-with-curl-works-its-version-with-guzzle-6-does-not%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

            Bressuire

            Vorschmack

            Quarantine