php 使用 CURL 将 JSON 数据发布到 API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18647611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:57:42  来源:igfitidea点击:

Posting JSON data to API using CURL

phpjsonapicurl

提问by ArkNet

When I'm posting jsondata to API using curl- I'm not getting any output. I would like to send email invitation to recipient.

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

$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);
}

And here is JSON$str_data

这里是 JSON$str_data

[
    {
        "authorizedKey"  : "abbad35c5c01-xxxx-xxx",
        "senderEmail"    : "[email protected]",
        "recipientEmail" : "[email protected]",
        "comment"        : "Invitation",
        "forceDebitCard" : "false"
    }
] 

And calling function:

并调用函数:

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

This is the API: https://api.payquicker.com/Help/Api/POST-api-SendInvitation

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

回答by Dylan Holmes

Try adding curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
And changing http_build_query($post)to $post

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

The implementation:

实施:

<?php

$data = array(
  "authorizedKey" => "abbad35c5c01-xxxx-xxx",
  "senderEmail" => "[email protected]",
  "recipientEmail" => "[email protected]",
  "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);

?>

The response I get is:

我得到的回应是:

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

But maybe it will work with valid data....

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

Edit:For posting xml, it's the same as on their site, except in a string:

编辑:对于发布 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>';

Then:

然后:

sendPostData($url_send, $xml)

回答by mousumi deb

You have to add header:

您必须添加标题:

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

And:

和:

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

Otherwise ...

除此以外 ...

HTTP Status 415 - Unsupported Media Type

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

... may happen.

……可能会发生。

回答by Sankar Palani

You don't need to add headers as you already do json_encode. just print_r (curl_getinfo($ch));and see the content type info in it.

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