本文介绍了使用curl发布JSON数据API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

$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"    : "[email protected]",
        "recipientEmail" : "[email protected]",
        "comment"        : "Invitation",
        "forceDebitCard" : "false"
    }
]

和调用函数:

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

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

推荐答案

尝试添加 curl_setopt($ CH,CURLOPT_FOLLOWLOCATION,1); 结果
和改变 http_build_query($ POST) $岗位

实施

<?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....

编辑:
张贴XML,
这是一样的在其网站上,除了在一个字符串:

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

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

这篇关于使用curl发布JSON数据API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 04:54