简体   繁体   English

Guzzle 的行为不像 CURL

[英]Guzzle not behaving like CURL

I want to migrate from pure CURL to Guzzle, but the API calls are not being registered correctly.我想从纯 CURL 迁移到 Guzzle,但未正确注册 API 调用。

Working CURL (Class from here: https://stackoverflow.com/a/7716768/8461611 )工作 CURL(来自这里的类: 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现在 Guzzle 的东西

...
$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.我想我在使用 cookie 时做错了什么。 They are being set as var_dump($jar);它们被设置为var_dump($jar); shows.显示。 API Docs : http://www.share-online.biz/uploadapi API 文档: http : //www.share-online.biz/uploadapi

First of all, Guzzle is not curl and cannot behave like curl.首先,Guzzle 不是 curl 并且不能像 curl 那样运行。 The only caveat is that it uses curl behind the scenes.唯一需要注意的是它在幕后使用 curl。

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我希望它可以帮助面临这种情况的其他人

First of all, you have to call ...->getBody()->getContents() to get a string.首先,您必须调用...->getBody()->getContents()来获取字符串。 Or cast body object to a string: (string) ...->getBody() .或者将 body 对象转换为字符串: (string) ...->getBody()

Then, you cannot use CurlFile class.然后,您不能使用CurlFile类。 Use fopen() to get a file handle and pass it directly to Guzzle like in the docs .使用fopen()获取文件句柄并将其直接传递给 Guzzle,就像在 docs 中一样 Pay attentions that for file uploads you have to use multipart instead of form_params .请注意,对于文件上传,您必须使用multipart而不是form_params

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM