unplugged_web Posted April 28, 2015 Share Posted April 28, 2015 I've very new to PHP and am trying to get API (REST) to work with a site I'm currently working on. It works if I submit a form using AJAX but I want it to work without using AJAX. The code I'm using is: <?php $postContactUrl = 'https://apiconnector.com/v2/contacts/'; $data = array( 'Email' => $_POST['email'], 'EmailType' => 'Html', 'dataFields' => array( array( 'Key' => 'FULLNAME', 'Value' => $_POST['name_first']." ".$_POST['name_last'] ), ) ); $contact = execute_post($postContactUrl, $data); $addContactToAddressBookUrl = 'https://apiconnector.com/v2/address-books/' . '123456' . '/contacts'; $book = execute_post($addContactToAddressBookUrl, $contact); function execute_post($url, $data){ $requestBody = json_encode($data, true); $ch = curl_init(); curl_setopt($ch, CURLAUTH_BASIC, CURLAUTH_DIGEST); curl_setopt($ch, CURLOPT_USERPWD, 'username' . ':' . 'password'); curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Accept: ' . 'application/json' ,'Content-Type: application/json')); $responseBody = json_decode(curl_exec($ch)); curl_close($ch); return $responseBody; } ?> but when I try to add that to the page the form is on I get this error: PHP Fatal error: Call to undefined function execute_post() I've tried to fix it but nothing I do seems to work. I don't understand why it works with AJAX but not any other way. I tried moving the function to before I called it but neither this: function execute_post(){ ... } $contact = execute_post($postContactUrl, $data); nor this worked: function execute_post(){ $contact = execute_post($postContactUrl, $data); Link to comment Share on other sites More sharing options...
justsomeguy Posted April 28, 2015 Share Posted April 28, 2015 Is that your exact code? It doesn't look like there's a problem there. Are you including that file or something? Link to comment Share on other sites More sharing options...
unplugged_web Posted April 29, 2015 Author Share Posted April 29, 2015 REST.phpYes that's in the rest of a file, I've attached the file as there's too much code to post here Link to comment Share on other sites More sharing options...
unplugged_web Posted April 29, 2015 Author Share Posted April 29, 2015 I think I have have discovered where the problem is. When I use print_r on $data it prints the results, $contact again works but $book prints nothing at all. I don't know what the reason for $book not showing anything. I tried renaming it incase it was conflicting with something I wasn't aware of but that didn't make any difference. Does anybody have any suggestion as to why this isn't working? Link to comment Share on other sites More sharing options...
unplugged_web Posted April 29, 2015 Author Share Posted April 29, 2015 Why is the $book variable empty? I've checked everything before it and up until that point everything is correct. I've even tried renaming it but that didn't work <?php $postContactUrl = 'https://apiconnector.com/v2/contacts/'; $data = array( 'Email' => $_POST['email'], 'EmailType' => 'Html', 'dataFields' => array( array( 'Key' => 'FULLNAME', 'Value' => $_POST['name_first']." ".$_POST['name_last'] ), ) ); $contact = execute_post($postContactUrl, $data); $addContactToAddressBookUrl = 'https://apiconnector.com/v2/address-books/' . '123456' . '/contacts'; $book = execute_post($addContactToAddressBookUrl, $contact); echo "<pre>" . print_r($book, true) . "</pre>"; ?> Link to comment Share on other sites More sharing options...
thescientist Posted April 29, 2015 Share Posted April 29, 2015 You've left out the most important part. Where's the code for execute_post? Link to comment Share on other sites More sharing options...
unplugged_web Posted April 29, 2015 Author Share Posted April 29, 2015 You've left out the most important part. Where's the code for execute_post? Sorry here it is: <?php//Function to initiate curl, set curl options, execute and return the responsefunction execute_post($url, $data){ //encode the data as json string $requestBody = json_encode($data, true); //initialise curl session $ch = curl_init(); //curl options curl_setopt($ch, CURLAUTH_BASIC, CURLAUTH_DIGEST); curl_setopt($ch, CURLOPT_USERPWD, 'username' . ':' . 'password'); curl_setopt($ch, CURLOPT_GETFIELDS, $requestBody); curl_setopt($ch, CURLOPT_GET, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Accept: ' . 'application/json' ,'Content-Type: application/json')); //curl execute and json decode the response $responseBody = json_decode(curl_exec($ch)); //close curl session curl_close($ch); return $responseBody;}?> Link to comment Share on other sites More sharing options...
justsomeguy Posted April 29, 2015 Share Posted April 29, 2015 Are you sure you're sending the correct values to the API? $contact is the result of sending $data to the API, then you turn around and send $contact right back? Is that correct? Does $contact contain the correct data to send to the API? Link to comment Share on other sites More sharing options...
justsomeguy Posted April 29, 2015 Share Posted April 29, 2015 Please don't create duplicate topics, I've merged them. Link to comment Share on other sites More sharing options...
unplugged_web Posted April 29, 2015 Author Share Posted April 29, 2015 Sorry I didn't mean to create duplicate post, I thought that they might not be similar enough. $contact contains the details of everybody in that book, $data contains the details from the form but $book is empty. This works perfectly if I call it like this: $.ajax({type: 'POST',url: '/api.php',data: $("#form").serialize(),success: function(data) {if(data == "true") {$("#form").fadeOut("fast", function(){$(this).before("<div class='thanks'><strong>Thank you you've been added to our mailing list</strong></div>");});}if(data == "false") {$("#form").fadeOut("fast", function(){$(this).before("<div class='thanks'><strong>You are already subscribed mailing list.</strong></div>");}); but this time I need to call it in the same file but it doesn't work Link to comment Share on other sites More sharing options...
justsomeguy Posted April 29, 2015 Share Posted April 29, 2015 That's jQuery code to send an ajax request, not the PHP code which sends the API request. What is different about the code that works and the code that doesn't? Link to comment Share on other sites More sharing options...
unplugged_web Posted April 30, 2015 Author Share Posted April 30, 2015 What is different about the code that works and the code that doesn't? There's no difference in the PHP at all. The only difference is one form calls is using JQuery (that one works) and the other is called on the same page (that doesn't work) Link to comment Share on other sites More sharing options...
unplugged_web Posted April 30, 2015 Author Share Posted April 30, 2015 I changed the (CURLOPT_GETFIELDS) and (CURLOPT_GET) to curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody); and it now works perfectly!! Link to comment Share on other sites More sharing options...
Ingolme Posted May 1, 2015 Share Posted May 1, 2015 There's a CURLOPT_POST option. CURLOPT_CUSTOMREQUEST is for when you want to use methods such as DELETE. There's no option GETFIELDS, but if you need a GET request you can put the data right into the URL as a query string. Link to comment Share on other sites More sharing options...
unplugged_web Posted May 1, 2015 Author Share Posted May 1, 2015 Am I wrong the use CURL_CUSTOMREQUEST? I read here that there's two options, delete and post. This is the first time I've used an API and cURL so have probably got it wrong. Link to comment Share on other sites More sharing options...
Ingolme Posted May 1, 2015 Share Posted May 1, 2015 There's no problem using the custom request option for POST requests, but cURL already has an option specifically for POST. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now