214

postJSONを使用した正しい方法を知っている人はいますGuzzleか?

$request = $this->client->post(self::URL_REGISTER,array(
                'content-type' => 'application/json'
        ),array(json_encode($_POST)));

サーバーからinternal server error応答があります。Chrome を使用して動作しPostmanます。

4

15 に答える 15

307

Guzzle 5、6、および 7 の場合、次のようにします。

use GuzzleHttp\Client;

$client = new Client();

$response = $client->post('url', [
    GuzzleHttp\RequestOptions::JSON => ['foo' => 'bar'] // or 'json' => [...]
]);

ドキュメント

于 2015-04-13T09:21:15.743 に答える
56

シンプルで基本的な方法 (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>';
于 2016-09-16T06:30:23.793 に答える
8

これは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')
                                )
                    );

documentation guzzleによると、json_encodeを実行します

于 2016-12-26T20:48:07.257 に答える
0

@ 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>';

    $request = $client->post($url, array('content-type' => 'application/xml',));

    $request->setBody($data); #set body! this is body of request object and not a body field in the header section so don't be confused.

    $response = $request->send(); #you must do send() method!
    echo $response->getBody(); #you should see the response body from the server on success
    die;

--- * Guzzle 6 *のソリューション --- -必要なパッケージ:

 "require": {
    "php"  : ">=5.5.0",
    "guzzlehttp/guzzle": "~6.0"
},

$client = new Client([
                             // Base URI is used with relative requests
                             'base_uri' => 'https://api.compay.com/',
                             // You can set any number of default request options.
                             'timeout'  => 3.0,
                             'auth'     => array('you@gmail.ca', 'dsfddfdfpassword', 'Digest'),
                             'headers' => array('Accept'        => 'application/vnd.comay.v1.1+xml',
                                                'Content-Type'  => 'text/xml'),
                         ]);

$url = "https://api.compay.com/cases.xml";
    $data string variable is defined same as above.


    // Provide the body as a string.
    $r = $client->request('POST', $url, [
        'body' => $data
    ]);

    echo $r->getBody();
    die;
于 2016-01-21T23:10:53.537 に答える
-1

@ user3379466 からの回答は$data、次のように設定することで機能させることができます。

$data = "{'some_key' : 'some_value'}";

私たちのプロジェクトで必要だったのは、変数を json 文字列内の配列に挿入することでした。これは次のように行いました (これが誰かの役に立つ場合に備えて)。

$data = "{\"collection\" : [$existing_variable]}";

したがって$existing_variable、たとえば 90210 の場合、次のようになります。

echo $data;
//{"collection" : [90210]}

'Accept' => 'application/json'また、ヒットしているエンドポイントがそのようなことを気にする場合に備えて、も設定することをお勧めします。

于 2014-07-17T23:44:02.397 に答える