2017-09-24 112 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