Home » Wordpress » PHP/Curl/WordPress Posting data without refreshing page, curl not working

PHP/Curl/WordPress Posting data without refreshing page, curl not working

Posted by: admin November 30, 2017 Leave a comment

Questions:

I am writing code for a donation page and want to submit CC information without refreshing the page, and display the results using an overlay. I have the overlay working correctly using jQuery, and using a php if statement, I can post back to the page I am on and get the variables correctly the the curl block for the CC transaction. However I never get any results. If I allow the page to POST/refresh it works fine, but it seems WordPress will not allow me to run the curl from a page that is not directly run in the enviroment.

Any suggestions?

jQuery.post('sameurl', jQuery("#donateform").serialize(), function(data) {
  jQuery('#overlay_msg').html(data);

and

        $ch = curl_init ();

        curl_setopt ($ch, CURLOPT_URL,$hoststring);
        curl_setopt ($ch, CURLOPT_POST, 1); 
        curl_setopt ($ch, CURLOPT_POSTFIELDS, $xml); # the string we built above
        curl_setopt ($ch, CURLOPT_SSLCERT, $cert);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt ($ch, CURLOPT_VERBOSE, 1);

        $result = curl_exec ($ch);

I hard coded the $xml string as well trying to get it to work. I’m new to php/curl/ajax but know a bit about coding in general.

Thanks.

Answers:

From what I can see your code is not calling any wordpress functions, so there is no dependency there. It sounds like the cURL call is not completing successfully, you will need to debug this. Make sure php error reporting is on and set to E_All in your php.ini, also set the curl option FAILONERROR to true and be sure to catch any errors using the curl error functions.

Failing this you may need to analyse the HTTP headers to see whats being received from the server. You may simply be getting a redirect try setting CURLOPT_FOLLOWLOCATION to true.

Questions:
Answers:

Well, it turns out it was a directory issue. WordPress sets you to the website root directory, while regular html has your directory relative to the file location. So I could not get my cert.