12

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

And here is JSON $str_data

[
    {
        "authorizedKey"  : "abbad35c5c01-xxxx-xxx",
        "senderEmail"    : "myemail@yahoo.com",
        "recipientEmail" : "jaketalledo86@yahoo.com",
        "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

Vaidas
  • 968
  • 9
  • 22
ArkNet
  • 157
  • 1
  • 1
  • 9

3 Answers3

24

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

The implementation:

<?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);

?>

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 = '
<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)
Dylan Holmes
  • 772
  • 4
  • 8
  • I just read on their api "_please append that to the url with ?authorizedKey=xxxxx_", I'll change the code above too – Dylan Holmes Sep 06 '13 at 00:38
  • do i need to assign that xxxx with my authentication key ?? – ArkNet Sep 06 '13 at 00:45
  • im still getting the {"success":false,"errorMessage":"Object reference not set to an instance of an object.","status":"N/A"} – ArkNet Sep 06 '13 at 00:47
  • i try to contact there support it staff as im still getting that error anyway thanks a lot for taking time to my question – ArkNet Sep 06 '13 at 01:09
  • Hope it works out for you! -I was thinking of trying to use xml instead of json, cause I tried one of their other api calls using xml and it worked. – Dylan Holmes Sep 06 '13 at 01:12
  • how do i call simplexml_load_file('data.xml') in sendPostData? – ArkNet Sep 06 '13 at 01:37
  • You don't need to. You should be able to send the xml as a string. However, I tried it with no success. It would be nice to know what they mean with the error message `Object reference not set to an instance of an object` ... – Dylan Holmes Sep 06 '13 at 01:44
  • can you post the code xml format and I try if its work.. thanks a lot – ArkNet Sep 06 '13 at 01:49
  • thanks a lot. I think this response from there server is something to do with.. System.Net.Mail on EXCHANGE Server. – ArkNet Sep 06 '13 at 02:43
13

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

... may happen.

Jason C
  • 38,729
  • 14
  • 126
  • 182
mousumi deb
  • 131
  • 1
  • 2
-1

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.

phrogg
  • 888
  • 1
  • 13
  • 28