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://stackoverflow.com/a/7716768/8461611)

...
$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

rndus2r
  • 496
  • 4
  • 17
  • What is the `CURL` class? Where does that come from? – Phil Sep 05 '17 at 04:53
  • @Phil https://stackoverflow.com/a/7716768/8461611 See edit in that post – rndus2r Sep 05 '17 at 04:57
  • Maybe not the answer you ask but I would recommend migrating to symfony http client instead of guzzle : https://symfony.com/doc/current/http_client.html https://www.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 plugable – job3dot5 Dec 18 '20 at 11:09

2 Answers2

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

Paul Tofunmi
  • 435
  • 4
  • 15
0

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.

Alexey Shokov
  • 4,775
  • 1
  • 21
  • 22
  • getBody() can be cast to string, which is implictly done (http://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. – rndus2r Sep 08 '17 at 00:27
  • @rndus2r, you are using Guzzle 6, but the docs are for Guzzle 3. Please, pay attention to this. – Alexey Shokov Sep 08 '17 at 10:18
  • When I was talking about `CurlFile`, I meant this parameter: `"fn" => new CurlFile(realpath($file)),`. – Alexey Shokov Sep 08 '17 at 10:19
  • You are right, link was outdated, here is the up-to-date link for implicit casts: http://docs.guzzlephp.org/en/stable/quickstart.html#using-responses I will give your second hint a shot – rndus2r Sep 08 '17 at 11:43