我如何使用Guzzle在JSON中发送POST请求?
有没有人知道使用Guzzle
post
JSON的正确方法?
$request = $this->client->post(self::URL_REGISTER,array( 'content-type' => 'application/json' ),array(json_encode($_POST)));
我从internal server error
得到一个internal server error
响应。 它使用Chrome Postman
。
对于5号和6号枪战,你可以这样做:
use GuzzleHttp\Client; $client = new Client(); $response = $client->post('url', [ GuzzleHttp\RequestOptions::JSON => ['foo' => 'bar'] ]);
文件
对于Guzzle <= 4 :
这是一个原始的发布请求,所以把JSON放在主体中解决了这个问题
$request = $this->client->post($url,array( 'content-type' => 'application/json' ),array()); $request->setBody($data); #set body! $response = $request->send(); return $response;
简单而基本的方法(guzzle6):
$client = new Client([ 'headers' => [ 'Content-Type' => 'application/json' ] ]); $response = $client->post('http://api.com/CheckItOutNow', ['body' => json_encode( [ 'hello' => 'World' ] )] );
为了得到响应状态码和身体的内容,我做了这个:
echo '<pre>' . var_export($response->getStatusCode(), true) . '</pre>'; echo '<pre>' . var_export($response->getBody()->getContents(), true) . '</pre>';
$client = new \GuzzleHttp\Client(); $body['grant_type'] = "client_credentials"; $body['client_id'] = $this->client_id; $body['client_secret'] = $this->client_secret; $res = $client->post($url, [ 'body' => json_encode($body) ]); $code = $res->getStatusCode(); $result = $res->json();
这对我有用(使用Guzzle 6)
$client = new Client(); $result = $client->post('http://api.example.com', [ 'json' => [ 'value_1' => 'number1', 'Value_group' => array("value_2" => "number2", "value_3" => "number3") ] ]); echo($result->getBody()->getContents());
这对Guzzle 6.2来说是适用的:
$gClient = new \GuzzleHttp\Client(['base_uri' => 'www.foo.bar']); $res = $gClient->post('ws/endpoint', array( 'headers'=>array('Content-Type'=>'application/json'), 'json'=>array('someData'=>'xxxxx','moreData'=>'zzzzzzz') ) );
根据文件guzzle做json_encode
@ user3379466的答案可以通过设置$data
,如下所示:
$data = "{'some_key' : 'some_value'}";
我们的项目需要的是将一个variables插入到jsonstring中的数组中,我这样做(如果这有助于任何人):
$data = "{\"collection\" : [$existing_variable]}";
所以用$existing_variable
(比如说90210)就可以得到:
echo $data; //{"collection" : [90210]}
另外值得注意的是,你也可能想要设置'Accept' => 'application/json'
,以防万一您碰到的端点在意这种事情。
上面的答案没有为我工作。 但是这对我来说工作正常。
$client = new Client('' . $appUrl['scheme'] . '://' . $appUrl['host'] . '' . $appUrl['path']); $request = $client->post($base_url, array('content-type' => 'application/json'), json_encode($appUrl['query']));
只要使用它,它将工作
$auth = base64_encode('user:'.config('mailchimp.api_key')); //API URL $urll = "https://".config('mailchimp.data_center').".api.mailchimp.com/3.0/batches"; //API authentication Header $headers = array( 'Accept' => 'application/json', 'Authorization' => 'Basic '.$auth ); $client = new Client(); $req_Memeber = new Request('POST', $urll, $headers, $userlist); // promise $promise = $client->sendAsync($req_Memeber)->then(function ($res){ echo "Synched"; }); $promise->wait();
@ user3379466是正确的,但在这里我完全重写:
-package that you need: "require": { "php" : ">=5.3.9", "guzzlehttp/guzzle": "^3.8" }, -php code (Digest is a type so pick different type if you need to, i have to include api server for authentication in this paragraph, some does not need to authenticate. If you use json you will need to replace any text 'xml' with 'json' and the data below should be a json string too): $client = new Client('https://api.yourbaseapiserver.com/incidents.xml', array('version' => 'v1.3', 'request.options' => array('headers' => array('Accept' => 'application/vnd.yourbaseapiserver.v1.1+xml', 'Content-Type' => 'text/xml'), 'auth' => array('username@gmail.com', 'password', 'Digest'),)));
$url = "https://api.yourbaseapiserver.com/incidents.xml"; $data = '<incident> <name>Incident Title2a</name> <priority>Medium</priority> <requester><email>dsss@mail.ca</email></requester> <description>description2a</description> </incident>';