2017-09-24 120 views
0
$client = new Client(); 
     $url = 'api-url'; 

     $request = $client->post($url, [ 
      'headers' => [ 'Content-Type' => 'application/json' ], 
      'json' => ['token' => 'foo'] 
     ]); 

     return $request; 

我也得到502 Bad Gateway和資源解釋爲文檔,但與MIME類型application/JSONUzing狂飲發送POST請求qith JSON

我需要做一些JSON POST請求轉移。如何在Guzzle和Laravel中做到這一點?

回答

0

試試看

$request = $client->post('http://api.example.com', [ 
    'json' => [ 
     'key' => 'value' 
    ] 
); 

dd($result->getBody()->getContents()); 
+0

@KiraArik其中狂飲的版本,您使用的? –

+0

這工作!非常感謝!!! – Kira

0

看看..

$client = new Client(); 

$url = 'api-url'; 

$headers = array('Content-Type: application/json'); 

$data = array('json' => array('token' => 'foo')); 

$request = new Request("POST", $url, $headers, json_encode($data)); 

$response = $client->send($request, ['timeout' => 10]); 

$data = $response->getBody()->getContents(); 
+0

我設法做到了。謝謝 – Kira