【问题标题】:how Can i post data to an url using curl and json?如何使用 curl 和 json 将数据发布到 url?
【发布时间】:2017-05-09 20:42:51
【问题描述】:

我想使用网站的 api 向我的应用发送通知 而且网站帮助不好:/

网站说你应该在请求tokan代码的标题和一些设置中写 如何使用 php 将 json 数据发布到该网站? 有了这些标题 “授权:令牌7fb1………………………………29b464c” “内容类型:应用程序/json” "接受:应用程序/json"

并且网址来自网站的帮助文本 curl -X POST "https://panel.pushe.co/api/v1/notifications/"

请帮帮我@_@

【问题讨论】:

  • 欢迎来到 Stack Overflow,在未来的问题中,请尝试更好地格式化您的问题(例如代码块中的代码),以便其他人更容易阅读。

标签: php json api curl post


【解决方案1】:

使用这样的东西:

$ curl -X POST -i -H "Content-Type: application/json" -H "Authorization: TOKEN <YOUR_TOKEN>" https://panel.pushe.co/api/v1/notifications/

【讨论】:

    【解决方案2】:

    来自here的php示例

    $data = array("name" => "Hagrid", "age" => "36");                                                                    
    $data_string = json_encode($data);                                                                                   
    
    $ch = curl_init('http://api.local/rest/users');                                                                      
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/json',                                                                                
        'Content-Length: ' . strlen($data_string))                                                                       
    );                                                                                                                   
    
    $result = curl_exec($ch);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-09
      • 2015-05-05
      • 2016-01-19
      • 2014-03-23
      • 2010-10-23
      • 2012-06-20
      相关资源
      最近更新 更多