doushang1890 2015-09-04 12:04
浏览 272

PHP cURL转换为Guzzle

I'm working on integration with sentione api. I have problem with guzzle, guzzle throws me 401 Unauthorized exception. I think it can be problem with post data, I tried to set it in another way, but I'm getting always the same result.

That's my code:

        $arr = array(
            'topicId' => id, 
            'from' => '2014-01-01 00:00:00.000 CET'
        );
        $params = json_encode($arr);

        $client = new Client('http://sentione.com/api', array(
            'request.options' => array(
                'headers' => array(
                        "Accept: application/json",        
                        "X-API-KEY: key",
                        "Content-type: application/json",
                    )
                )
        ));

        $request = $client->post('statements/search', array('Content-Type' => 'application/json'), $params);
        $response = $request->send();

And that's my cURL definition:

$arr = array(
    "topicId" => id, 
    "from" => "2014-01-01 00:00:00.000 CET"
);

$json = json_encode($arr);

$headers = array(
        "Accept: application/json",        
        "X-API-KEY: key",
        "Content-type: application/json",
);
$url = "http://sentione.com/api/statements/search";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
$response = curl_exec($curl); 

Of course it's working great. Anybody can help me? Maybe I forgot sth?

  • 写回答

1条回答 默认 最新

  • douyan1921 2015-09-15 11:06
    关注

    I think problem is in this construction:

        $client = new Client('http://sentione.com/api', array(
            'request.options' => array(
                'headers' => array(
                        "Accept: application/json",        
                        "X-API-KEY: key",
                        "Content-type: application/json",
                    )
                )
        ));
    

    There is information about difference between versions of Guzzle client in part of creating requests. Check here:

    https://github.com/guzzle/guzzle/issues/777

    Why is my Authorization Header giving me a 401 in Guzzle?

    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)