【问题标题】:Guzzle php: Unable to make a Guzzle post requestGuzzle php:无法发出 Guzzle 发布请求
【发布时间】:2014-08-20 16:47:51
【问题描述】:

我的正常工作的卷发如下所示:

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $authenticateUrl);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($user));
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

    $response = curl_exec($curl);

    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

    curl_close($curl);

但是,我的 Guzzle 无法正常工作,如下所示:

    $client = new \GuzzleHttp\Client();
    $client->setDefaultOption('verifyPeer',false);

    $response = $client->post($authenticateUrl,array(
        'header' => $headers,
        'body'   => json_encode($user)
        )
    );

    $status = $response->getStatusCode();

我在 Guzzle post call 中做错了什么?

【问题讨论】:

  • 您是否收到任何错误消息?

标签: php curl laravel-4 guzzle


【解决方案1】:
        $request = $client->createRequest('POST', $authenticateURL);
        $request->setHeader('KEY', 'VALUE');
        $request->setBody('data');
        $response = $client->send($request);
        echo $response->getStatusCode();
        echo $response->getReasonPhrase();

首先我希望你的 $headers 是一个关联数组

如果您发现上述解决方案不起作用,请尝试使用文档中提到的这种方法`

$client->get('/get', [
'headers' => [
    'User-Agent' => 'testing/1.0',
    'Accept'     => 'application/json',
    'X-Foo'      => ['Bar', 'Baz']
]
]);

我想知道浏览器中的错误报告,因为有很多错误导致代码无法正常工作。但是尝试我提到的第一种方法,它是我更喜欢做的第一个也是最重要的方法,而不是传递所有一行指令中的数据。这种方式使我可以轻松调试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-15
    • 1970-01-01
    • 2020-04-12
    • 2014-10-09
    • 1970-01-01
    • 2019-05-31
    • 1970-01-01
    相关资源
    最近更新 更多