Posting JSON data to API using CURL

后端 未结 3 1713
逝去的感伤
逝去的感伤 2021-01-04 10:15

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.

$         


        
3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-04 10:35

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

    The implementation:

     "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 = '
    
      80c587b9-caa9-4e56-8750-a34b17dba0a2
      sample string 4
      true
      sample string 3
      sample string 2
    ';
    

    Then:

    sendPostData($url_send, $xml)
    

提交回复
热议问题