2017-05-24 437 views
1

我試試這樣的郵差:如何從guzzle使用auth和body raw獲得響應?

我只填寫輸入密碼。然後我點擊按鈕更新請求

這樣的視圖:

enter image description here

這是標頭:

enter image description here

這是身體。我選擇原始輸入數據是這樣的:

enter image description here

然後我點擊按鈕來發送,它可以得到的迴應

但是當我嘗試使用狂飲6這樣的:

public function testApi() 
{ 
    $requestContent = [ 
     'headers' => [ 
      'Accept' => 'application/json', 
      'Content-Type' => 'application/json' 
     ], 
     'json' => [ 
      'auth' => ['', 'b0a619c152031d6ec735dabd2c6a7bf3f5faaedd'], 
      'ids' => ["1"] 
     ] 
    ]; 
    try { 
     $client = new GuzzleHttpClient(); 
     $apiRequest = $client->request('POST', 'https://myshop/api/order', $requestContent); 
     $response = json_decode($apiRequest->getBody()); 
     dd($response); 
    } catch (RequestException $re) { 
      // For handling exception. 
    } 
} 

結果爲空

我該如何解決這個問題?

回答

1

請參閱Postman,您在「標題」選項卡中正確指定了字段Authorization。所以當你使用Guzzle時,應該是一樣的,把它放在標題中:

public function testApi() 
{ 
    $requestContent = [ 
     'auth' => ['username', 'password'] 
     'headers' => [ 
      'Accept' => 'application/json', 
      'Content-Type' => 'application/json', 
     ], 
     'json' => [ 
      'ids' => ["1"] 
     ] 
    ]; 

    try { 
     $client = new GuzzleHttpClient; 
     $apiRequest = $client->request('POST', 'https://myshop/api/order', $requestContent); 
     $response = json_decode($apiRequest->getBody()); 
     dd($response); 
    } catch (RequestException $re) { 
      // For handling exception. 
    } 
} 
+0

看來我的服務器有問題了。所以我不能嘗試它。稍後當我嘗試它,它的工作。我會接受你的回答 –

+0

好的,讓我知道。我編輯了我的答案,因爲我忘記了'Content-Type'=>'application/json'這行末尾的逗號。 – louisfischer

+0

我試過了。但它不起作用。它重定向到頁面登錄。它似乎不太認證。我在你的編碼中看到,沒有認證。但在我展示的郵差中,這是驗證碼 –