Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guzzle not behaving like CURL

Tags:

php

curl

guzzle6

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

like image 391
rndus2r Avatar asked Sep 03 '17 03:09

rndus2r


People also ask

Does Guzzle use cURL?

Guzzle has historically only utilized cURL to send HTTP requests. cURL is an amazing HTTP client (arguably the best), and Guzzle will continue to use it by default when it is available. It is rare, but some developers don't have cURL installed on their systems or run into version specific issues.

What is difference between Guzzle and cURL?

Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services. cURL can be classified as a tool in the "File Transfer" category, while Guzzle is grouped under "Microframeworks (Backend)". cURL and Guzzle are both open source tools.

Is Guzzle asynchronous?

Guzzle allows you to send both asynchronous and synchronous requests using the same interface and no direct dependency on an event loop. This flexibility allows Guzzle to send an HTTP request using the most appropriate HTTP handler based on the request being sent.


1 Answers

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

like image 106
Paul Tofunmi Avatar answered Sep 23 '22 15:09

Paul Tofunmi