Guzzle not behaving like CURL
Asked Answered
D

2

15

I want to migrate from pure CURL to Guzzle, but the API calls are not being registered correctly.

Working CURL (Class from here: https://mcmap.net/q/826501/-passing-cookie-using-curl)

...
$Curl = new CURL(); // setting all curl_opts there

// creating session
$session = explode(";", $Curl->post("http://www.share-online.biz/upv3_session.php", "username=".$un."&password=".$pw));
$session_key = $session[0];
$upload_server = $session[1];

// upload
$vars = ... // see below
var_dump(explode(";",$Curl->post($upload_server, $vars))); // works

Now the Guzzle stuff

...
$Curl = new GuzzleHttp\Client();
$jar = new GuzzleHttp\Cookie\FileCookieJar("cookie.txt", true);

//creating session

$session = explode(";", $Curl->request('POST', "http://www.share-online.biz/upv3_session.php", 
   ["form_params" => ["username" => $un, "password" => $pw], 'cookies' => $jar])->getBody());
$session_key = $session[0];
$upload_server = $session[1];

$vars = ["username" => $un,
            "password" => $pw,
            "upload_session" => $session_key,
            "chunk_no" => 1,
            "chunk_number" => 1,
            "filesize" => filesize($file),
            "fn" => new CurlFile(realpath($file)),
            "finalize" => 1,
            "name" => "test",
            "contents" => $file,
    ];

var_dump(
    explode(";",$Curl->request(
            'POST', "http://".$upload_server, ["multipart" => [$vars], 'cookies' => $jar])
               ->getBody()));
// outputs *** EXCEPTION session creation/reuse failed - 09-3-2017, 3:05 am ***

I assume I'm doing something wrong with cookies. They are being set as var_dump($jar); shows. API Docs : http://www.share-online.biz/uploadapi

Deposal answered 3/9, 2017 at 3:12 Comment(3)
What is the CURL class? Where does that come from?Frantz
@Frantz https://mcmap.net/q/826501/-passing-cookie-using-curl See edit in that postDeposal
Maybe not the answer you ask but I would recommend migrating to symfony http client instead of guzzle : symfony.com/doc/current/http_client.html youtube.com/watch?v=CYapIiL7Vqg&ab_channel=AFUPPHP It's more efficient will be maintained over years and more migration friendly, also of course easily symfony plugableBandur
C
1

First of all, you have to call ...->getBody()->getContents() to get a string. Or cast body object to a string: (string) ...->getBody().

Then, you cannot use CurlFile class. Use fopen() to get a file handle and pass it directly to Guzzle like in the docs. Pay attentions that for file uploads you have to use multipart instead of form_params.

Caceres answered 5/9, 2017 at 13:45 Comment(4)
getBody() can be cast to string, which is implictly done (guzzle3.readthedocs.io/http-client/response.html#response-body) ; $file is a string in this case, which, according to docs, should be allowed. But nothing changed when wrapped around with fopen(). Same error as above sadly. multipart is set.Deposal
@rndus2r, you are using Guzzle 6, but the docs are for Guzzle 3. Please, pay attention to this.Caceres
When I was talking about CurlFile, I meant this parameter: "fn" => new CurlFile(realpath($file)),.Caceres
You are right, link was outdated, here is the up-to-date link for implicit casts: docs.guzzlephp.org/en/stable/quickstart.html#using-responses I will give your second hint a shotDeposal
C
1

First of all, Guzzle is not curl and cannot behave like curl. The only caveat is that it uses curl behind the scenes.

Here is the solution:

use GuzzleHttp\Client;

$client = new Client([
    // Base URI is used with relative requests
    'base_uri' => 'http://www.share-online.biz/',
    'timeout'  => 2.0,
]);


$response = $client->request('POST', 'upv3_session.php', 
   [
      'form_params' => [
             "username" => $un, 
             "password" => $pw
      ]
   ]
);

Use the output of your request like so: 
$code = $response->getStatusCode(); // 200 || 400 | 500 etc
$reason = $response->getReasonPhrase(); 
$body = $response->getBody();

$response = $request->getBody(); //Explicitly cast to string. 
$json_response = json_decode($response); //here the string response has be decoded to json string.

I hope it helps others that facing this situation

Cephalic answered 15/5, 2020 at 8:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.