duanpin5168 2017-02-16 08:58
浏览 75
已采纳

在cur中将curl转换为guzzle代码

I have a curl code like this which I am trying to convert into guzzle like so

$response = $client->post(self::$url, [
        'query' => array(
            'app_id' => "app-id",
            'included_segments' => array('All'),
            'contents' => $content,
            'headings' => $headings) ],
              ['headers' => [
                      'Content-Type' => 'application/json',
                      'Authorization' => 'Basic api key'
              ]
        ]);

But when I try to run this I get this error

...` resulted in a `400 Bad Request` response:
{\"errors\":[\"Please include a case-sensitive header of Authorization: Basic <YOUR-REST-API-KEY-HERE> with a valid REST AP (truncated...)

CURL

curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8','Authorization: Basic api key'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
  • 写回答

1条回答 默认 最新

  • dsh77114 2017-02-16 10:06
    关注

    Which version of Guzzle is this? Because the latest is different.

    $client = new GuzzleHttp\Client();
    $req = $client->request('POST', self::$url, [
        'json' => ['app_id' => '...', 'foo' => 'bar'],
        'headers' => ['Authorization' => 'Basic api key']
    ]);
    $res = $client->getBody()->getContents();
    

    I'm pretty sure that 'json' adds automatically the specific header, otherwise transform 'json' in 'form_params' and add the header (content-type).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用