簡體   English   中英

狂飲6.x /未獲得預期結果

[英]Guzzle 6.x / Not getting expected result

我有一個寧靜的API。 當我在以下URL上運行POSTMAN時,收到的響應如下:

POSTMAN RUN URL
DELETE
https://www.example.com/api/v1/Blog/blog/13


      {
      "status":"Failure",
      "message":"The specified blog post could not be found"
      }

以上當然是可以預期的,但是,我無法閱讀“狀態”和“消息”。 我如何獲得該回復? 這是我目前的代碼:

      $entry_id = $this->uri->segment(3);
      $theUrl = $this->config->item('base_url').'api/v1/Blog/blog/'.$entry_id;
      // tested $theUrl and works 
      $client = new GuzzleHttp\Client([
        'base_uri' =>       $theUrl,
        'timeout'  =>       3.0,
        'http_errors' =>    FALSE
        ]);
      $response = $client->delete($theUrl);
      $code = $response->getStatusCode();
      $response = $client->delete($theUrl); 
      $x = $response->getBody();

 echo "<pre>";
 echo var_dump($x);  // cannot see message or status anywhere.
 echo "</pre>";

非常感謝您的建議。

+++我現在已經嘗試過修改后的代碼,但仍然無法在回復中看到狀態或消息日期:

    $entry_id = $this->uri->segment(3);

    $theUrl = $this->config->item('base_url').'api/v1/Blog/blog/'.$entry_id;
    $client = new GuzzleHttp\Client([
        'timeout'  =>       3.0,
        'http_errors' =>    FALSE
        ]);
    $response = $client->delete($theUrl, ['debug' => true]);
    $code = $response->getStatusCode();
    $x = $response->getBody();

    echo "<pre>";
    echo var_dump($x);
    echo "</pre>";

    die();

這是轉儲和調試信息:

 https://www.example.com/api/v1/Blog/blog/6
 * Hostname was found in DNS cache * Trying 104.131.132.25... * Connected to      
 www.example.com (104.131.132.25) port 443 (#1) * successfully set  
 certificate verify locations: * CAfile: none CApath: /etc/ssl/certs * SSL 
 connection using XXXXXXXXXXXXXXXXXXXXXXXXX* Server certificate: * 
 subject: CN=www.example.com * start date: 2016-10-29 05:15:00 GMT * expire 
 date: 2017-01-27 05:15:00 GMT * subjectAltName: www.movinghaus.com matched 
 * issuer: C=US; O=Let's Encrypt; CN=Let's Encrypt Authority X3 * SSL 
 certificate verify ok. > DELETE /api/v1/Blog/blog/6 HTTP/1.1 User-Agent: 
 GuzzleHttp/6.2.0 curl/7.35.0 PHP/5.5.9-1ubuntu4.11 Host: www.example.com < 
 HTTP/1.1 200 OK < Date: Fri, 04 Nov 2016 03:55:21 GMT * Server Apache/2.4.7 
 (Ubuntu) is not blacklisted < Server: Apache/2.4.7 (Ubuntu) < X-Powered-By: 
 PHP/5.5.9-1ubuntu4.11 < Set-Cookie: 
 PHPSESSID=XXXXXXXXXXXXXXXXXX; expires=Fri, 04-Nov-2016 05:55:21 GMT; Max-
 Age=7200; path=/; HttpOnly < Expires: Thu, 19 Nov 1981 08:52:00 GMT < 
 Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-
 check=0 < Pragma: no-cache < Content-Length: 40 < Content-Type: 
 application/json; charset=utf-8 < * Connection #1 to host www.example.com 
 left intact


 object(GuzzleHttp\Psr7\Stream)#65 (7) {
 ["stream":"GuzzleHttp\Psr7\Stream":private]=>
 resource(50) of type (stream)
 ["size":"GuzzleHttp\Psr7\Stream":private]=>
 NULL
 ["seekable":"GuzzleHttp\Psr7\Stream":private]=>
 bool(true)
 ["readable":"GuzzleHttp\Psr7\Stream":private]=>
 bool(true)
 ["writable":"GuzzleHttp\Psr7\Stream":private]=>
 bool(true)
 ["uri":"GuzzleHttp\Psr7\Stream":private]=>
 string(10) "php://temp"
 ["customMetadata":"GuzzleHttp\Psr7\Stream":private]=>
 array(0) {
 }
 }

我認為不需要新的GuzzleHttp \\ Client的base_uri參數。 base_uri參數用於設置基本URL。 http://docs.guzzlephp.org/en/latest/quickstart.html?highlight=base_uri )然后,對客戶端的所有調用都應使用相對uris。 由於在delete函數調用中使用的是絕對URL,因此您不需要base_uri參數。

另外,您兩次調用delete函數:$ response = $ client-> delete($ theUrl)。

在根據文檔 你應該做

$x = $response->getBody()->getContents();

或將bodystring

$x = (string)$response->getBody()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM