【问题标题】:Array and Guzzle阵列和暴饮暴食
【发布时间】:2017-04-28 06:45:14
【问题描述】:

在 symfony 控制台应用程序中遇到 guzzle 和 json 问题,我的代码如下所示:

$client = new Client([
    'timeout' => 15,0,
]);

$body =[
    "revision" => "1",
    "changelog" => "stuff",
    "description" => "Testing",
    "user" => "Foo bar",
];
var_dump(json_encode($body));

$request = new GuzzleRequest('POST', "https://api.newrelic.com/v2/applications/$appId/deployments.json", array(), ['deployment' => json_encode($body)]);

$response = $client->send($request, ['headers' => ['X-Api-Key' => $apiKey]], ['timeout' => 200]);

我得到的回应是:

 Invalid resource type: array

【问题讨论】:

  • 查看您的日志并粘贴完整的错误代码。
  • 日志中没有这方面的内容。
  • 好像应该是$request = new GuzzleRequest('POST', "https://api.newrelic.com/v2/applications/$appId/deployments.json", ['deployment' => json_encode($body)]);
  • 不,那里需要那个数组(它用于标题)。
  • 对于 curl 它是这样的:` curl -X POST 'api.newrelic.com/v2/applications/someappid/deployments.json' \ -H 'X-Api-Key:somkey' -i \ -H 'Content-Type: application/json' \ -d \ '{ "部署": { "revision": "1", "changelog": "stuff", "description": "Testing", "user": "Mikke" } }' `

标签: php symfony


【解决方案1】:

整理好了:

$body = ['deployment' => [
    "revision" => "1",
    "changelog" => "stuff",
    "description" => "Testing",
    "user" => "Foo bar",
  ]];


$client = new Client([
  'timeout' => 15,0,
]);

$request = new GuzzleRequest(
  'POST',
  "https://api.newrelic.com/v2/applications/$appId/deployments.json",
  ["content-type" => 'application/json'],
  json_encode($body)
);
$response = $client->send($request, ['headers' => ['X-Api-Key' => $apiKey]], ['timeout' => 200]);

【讨论】:

    【解决方案2】:

    根据this(Guzzle6),必须指定content-type,试试:

    $client = new Client([
        'timeout' => 15,0,
    ]);
    
    $body = [ 'deployment' => [ 
                 "revision" => "1", 
                 "changelog" => "stuff", 
                 "description" => "Testing", 
                 "user" => "Foo bar", 
               ] 
            ];
    
    $request = new GuzzleRequest('POST', "https://api.newrelic.com/v2/applications/$appId/deployments.json", ["content-type"=>'application/json'], json_encode($body));
    
    $response = $client->send($request, ['headers' => ['X-Api-Key' => $apiKey]], ['timeout' => 200]);
    

    希望这会有所帮助!

    【讨论】:

    • 你使用的 Guzzle 是什么版本的?
    • 尝试发送json_encode($body)作为最后一个参数
    • 最后一个参数是 json_encode($body) - 还是我想念你?稍微修改了代码,但同样的问题。 ``$body = ['deployment' => [“revision”=>“1”,“changelog”=>“stuff”,“description”=>“Testing”,“user”=>“Foo bar”,] ]; $client = new Client([ 'timeout' => 15,0, ]); $request = new GuzzleRequest('POST', "api.newrelic.com/v2/applications/$appId/deployments.json", array(), ['json' => $body]); $response = $client->send($request, ['headers' => ['X-Api-Key' => $apiKey]], ['timeout' => 200]);\
    猜你喜欢
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 2011-04-27
    • 1970-01-01
    • 2018-06-19
    • 1970-01-01
    • 2015-01-22
    相关资源
    最近更新 更多