Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用CURL将JSON数据发布到API_Php_Json_Api_Curl - Fatal编程技术网

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

Php 使用CURL将JSON数据发布到API,php,json,api,curl,Php,Json,Api,Curl,当我使用curl将json数据发布到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
json
数据发布到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
$stru data

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

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

这是API:

尝试添加
curl\u setopt($ch,curloopt\u FOLLOWLOCATION,1)
并将
http\u build\u查询($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);

?>
但也许它会在有效数据下工作

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

您必须添加标题:

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

否则

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


。。。可能发生。

您不需要像以前那样添加头
json\u encode

只是
print_r(curl_getinfo($ch))并查看其中的内容类型信息。

我刚刚在他们的api上读到“请用?authorizedKey=xxxxx将其附加到url”,我将更改上面的代码,我需要用我的身份验证密钥分配该xxxx吗?我仍然收到{“success”:false,“errorMessage”:“对象引用未设置为对象的实例。”,“status”:“N/A”}我试着联系那里的it支持人员,因为我仍然会遇到这个错误。非常感谢你花时间回答我的问题。希望你能解决这个问题-我想尝试使用xml而不是json,因为我尝试了他们使用xml的另一个api调用,结果成功了。
sendPostData($url_send, $xml)
$headers= array('Accept: application/json','Content-Type: application/json'); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);