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
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With