duansao6776 2014-10-18 14:16
浏览 38

curl post api对api的请求没有使用php

I am trying to send a push notification using my own API using below code. I am sure curl was enabled in my server. I am getting below as a response.

        $url = "http://efpushtest.meteor.com/api/push";

        # Our new data
        $data = json_encode(array(
                'message' => "this is venkat test push message",
                'device' => "12345"
        ));

        $ch = curl_init($url);

        # Setting our options
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        # Get the response
        $result = curl_exec($ch);
        curl_close($ch);

Response:

{"success":false,"message":"Unexpected token u"}

I am not getting where i am going wrong with this code.

  • 写回答

2条回答 默认 最新

  • dshmvqnl98119 2014-10-18 14:25
    关注

    Since you say in the comment that it is expecting post parameters, your request should not be json encoded as in your code. A proper way to handle that would be to post the fields like so:

        $url = "http://efpushtest.meteor.com/api/push";
    
        # Our new data
        $data = array(
                'message' => "this is venkat test push message",
                'device' => "12345"
        );
    
        $ch = curl_init($url);
    
        // build the post string here
        foreach($data as $key=>$value) { 
            $fields_string .= $key.'='.$value.'&'; 
        }
    
        rtrim($fields_string, '&');
    
        # Setting our options
        curl_setopt($ch, CURLOPT_POST, count($data));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
        # Get the response
        $result = curl_exec($ch);
        curl_close($ch);
    

    where you are actually posting the parameters as POST parameters passing them in the form a string as you would see in GET. Your example would post:

    message=this is venkat test push message&device=12345

    If i were you, i would also update the array declaration so it passes url_encoded values to the post as in:

        $data = array(
                'message' => urlencode("this is venkat test push message"),
                'device' => urlencode("12345")
        );
    

    to prevent any special characters in the string to break your requests

    OLD ANSWER FOR JSON

    Try setting the headers of the curl request to specify that you are posting a `json` to the api # Setting our options curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计