【问题标题】:Posting JSON data to API using CURL使用 CURL 将 JSON 数据发布到 API
【发布时间】:2013-09-09 22:53:44
【问题描述】:

当我使用 curljson 数据发布到 API 时 - 我没有得到任何输出。我想向收件人发送电子邮件邀请。

$url_send ="http://api.address.com/SendInvitation?";
$str_data = json_encode($data);

function sendPostData ($url, $post) {

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));

    return curl_exec($ch);
}

这里是JSON $str_data

[
    {
        "authorizedKey"  : "abbad35c5c01-xxxx-xxx",
        "senderEmail"    : "myemail@yahoo.com",
        "recipientEmail" : "jaketalledo86@yahoo.com",
        "comment"        : "Invitation",
        "forceDebitCard" : "false"
    }
] 

以及调用函数:

$response = sendPostData($url_send, $str_data);

这是 API:https://api.payquicker.com/Help/Api/POST-api-SendInvitation

【问题讨论】:

    标签: php json api curl


    【解决方案1】:

    您不需要像 json_encode 那样添加标题。 只需print_r (curl_getinfo($ch)); 并查看其中的内容类型信息。

    【讨论】:

      【解决方案2】:

      你必须添加标题:

      $headers= array('Accept: application/json','Content-Type: application/json'); 
      

      还有:

      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
      

      否则...

      HTTP 状态 415 - 不支持的媒体类型

      ...可能会发生。

      【讨论】:

        【解决方案3】:

        尝试添加curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        并将http_build_query($post) 更改为$post

        实现:

        <?php
        
        $data = array(
          "authorizedKey" => "abbad35c5c01-xxxx-xxx",
          "senderEmail" => "myemail@yahoo.com",
          "recipientEmail" => "jaketalledo86@yahoo.com",
          "comment" => "Invitation",
          "forceDebitCard" => "false"
        );
        
        $url_send ="http://api.payquicker.com/api/SendInvitation?authorizedKey=xxxxx";
        $str_data = json_encode($data);
        
        function sendPostData($url, $post){
          $ch = curl_init($url);
          curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
          curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
          $result = curl_exec($ch);
          curl_close($ch);  // Seems like good practice
          return $result;
        }
        
        echo " " . sendPostData($url_send, $str_data);
        
        ?>
        

        我得到的回应是:

        {"success":false,"errorMessage":"Object reference not set to an instance of an object.","status":"N/A"}
        

        但也许它适用于有效数据....

        编辑: 对于发布 xml, 它和他们网站上的一样,除了在一个字符串中:

        $xml = '
        <SendInvitationRequest xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/PQApi.Models">
          <authorizedKey>80c587b9-caa9-4e56-8750-a34b17dba0a2</authorizedKey>
          <comment>sample string 4</comment>
          <forceDebitCard>true</forceDebitCard>
          <recipientEmail>sample string 3</recipientEmail>
          <senderEmail>sample string 2</senderEmail>
        </SendInvitationRequest>';
        

        然后:

        sendPostData($url_send, $xml)
        

        【讨论】:

        • 我刚刚在他们的 api 上读到“请使用 ?authorizedKey=xxxxx 将其附加到 url”,我也会更改上面的代码
        • 我需要用我的身份验证密钥分配那个 xxxx 吗??
        • 我仍然得到 {"success":false,"errorMessage":"Object reference not set to an instance of an object.","status":"N/A"}
        • 我尝试联系那里的支持人员,因为无论如何我仍然遇到该错误,非常感谢您花时间回答我的问题
        • 希望对你有用! -我正在考虑尝试使用 xml 而不是 json,因为我尝试了使用 xml 的其他 api 调用之一并且它起作用了。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多