有人知道用Guzzle发布JSON的正确方法吗?

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

我从服务器得到一个内部服务器错误响应。它使用Chrome邮差工作。


当前回答

对于《Guzzle 5》,《Guzzle 6》和《Guzzle 7》,你是这样做的:

use GuzzleHttp\Client;

$client = new Client();

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

Docs

其他回答

对于Guzzle <= 4:

这是一个原始的post请求,所以把JSON放在body中解决了这个问题

$request = $this->client->post(
    $url,
    [
        'content-type' => 'application/json'
    ],
);
$request->setBody($data); #set body!
$response = $request->send();

解决方案$客户端->请求('POST',…

对于那些使用$client->请求的人,这是如何创建JSON请求的:

$client = new Client();
$res = $client->request('POST', "https://some-url.com/api", [
    'json' => [
        'paramaterName' => "parameterValue",
        'paramaterName2' => "parameterValue2",
    ]
    'headers' => [
    'Content-Type' => 'application/json',
    ]
]);

Guzzle JSON请求参考

@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 = '<事件> <名称>事件Title2a < /名称> <优先>中> < /优先 <请求者> < >电子邮件dsss@mail.ca < /电子邮件> < /请求者> <描述> description2a > < /描述 > < /事件”;

    $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;

解决*暴饮暴食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;

简单地使用它将工作

   $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的答案可以通过设置$data来工作,如下所示:

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

我们的项目需要的是将一个变量插入到json字符串中的数组中,我这样做如下(如果这有助于任何人):

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

因此,与$existing_variable是,说,90210,你得到:

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

另外值得注意的是,你可能还想设置'Accept' => 'application/json'以防你碰到的端点关心这类事情。