We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x
Private

Wordpress - Javascript - JQuery - AJAX - PHP - need assitance connecting AJAX request with Curl Call

High Priority
12 Views
Last Modified: 2020-06-26
Need assistance with AJAX request and Curl Call.

We have created a button that performs an AJAX request. I need that request to pull some of the active user's account information from the WordPress database and send it via a curl call to a specified URL to receive a response.
Comment
Watch Question

Netty AdminNetwork Administrator

Author

Commented:
Below is the Functions.php file

<?php
/*
 * Custom PHP code for child theme will be here
 */



/**
 *  the AJAX handler
 */
add_action('wp_ajax_get_order_url', 'get_order_url');
function get_order_url()
{
    check_ajax_referer('security_matters', 'security');
    $data = [
        'message' => 'This has come from the Servers AJAX Handler',
    ];
    wp_send_json($data);
}


/*
 * Enqueue scripts for cc button on checkout page
 */

add_action( 'wp_enqueue_scripts', 'setup_order_scripts', 15 );
 function setup_order_scripts() {
    $params = [
        'url' => admin_url( 'admin-ajax.php' ),
        'nonce' => wp_create_nonce('security_matters'),
    ];
    wp_register_script( 'cc_order_scripts', get_stylesheet_directory_uri() . '/order-scripts.js', array('jquery'), null, true);
    wp_localize_script( 'cc_order_scripts', 'ccOrderParams', $params);
    wp_enqueue_script( 'cc_order_scripts' );
}


/** Adding child theme's style.css **/
function tm_fixology_child_style_css(){
 wp_enqueue_style( 'fixology-child-style', get_stylesheet_directory_uri() . '/style.css' );
}
add_action( 'wp_enqueue_scripts', 'tm_fixology_child_style_css', 18 );



// Disable auto-complete on form.
add_filter( 'gform_form_tag', function( $form_tag ) {
 return str_replace( '>', ' autocomplete="off">', $form_tag );
}, 11 );
// Diable auto-complete on each field.
add_filter( 'gform_field_content', function( $input ) {
 return preg_replace( '/<(input|textarea)/', '<${1} autocomplete="off" ', $input );
}, 11 );
Netty AdminNetwork Administrator

Author

Commented:
Below is the JavaScript file in in use order-scripts.php


jQuery( function( $ ) {
    $(document).ready( function() {
        /**
         * AJAX Call to grab the Order URL from the server via the custom url coworker created in Azure logics
         */
        $( document.body ).on( "click", "#payment #get_cc_order_url.get_cc_order_url", function(e) {
            e.preventDefault();
            $.ajax({
                url  : ccOrderParams.url,
                data : { action: 'get_order_url' },
                type : 'post',
            }).done(function(response) {
                console.log(response)
            }).fail(function(error) {
                console.log("ERROR", error)
            })
        });
    });
})
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019
Hi Netty,

Based on your previous question (29186654) we can now have a go at getting the curl request done.
Netty AdminNetwork Administrator

Author

Commented:

To get the active user info:

$user = wp_get_current_user();
$id = $user->ID;
$email = $user->user_email;
$username = $user->user_login;

Just need to figure out how to pull the billing zip code:
Netty AdminNetwork Administrator

Author

Commented:
Excellent---- Thank you, I am ready to go
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Netty AdminNetwork Administrator

Author

Commented:
Ok I ran it and got the following In console:


Netty AdminNetwork Administrator

Author

Commented:

Netty AdminNetwork Administrator

Author

Commented:

I like the new simple clean function for the user data and I like this whole setup. I am learning a lot here.
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Netty AdminNetwork Administrator

Author

Commented:
I am not getting any errors with that code either......The red error above it is non-related, it is some google fonts error..



Netty AdminNetwork Administrator

Author

Commented:
Its working now!! he did not have his response coded to come back in JSON but he changed it and now it is working!!
Netty AdminNetwork Administrator

Author

Commented:

CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019
Ahh OK. I though the error was related to the AJAX call,

Right - Like  Isaid, we may need to change that last line based on what's coming back from your API call, so let's try that. Try this instead:

if (!curl_errno($curl)) {
    $response = ['api_result'=> $response];
    wp_send_json_success($response);
} else {
    $response = ['error' => curl_error($curl)];
    wp_send_json_error($response);
}

Open in new window

The difference here is that we're sending the original response through without json_decoding it. If the API doesn't send JSON then the original code would fail, so let's try this. Once we know exactly what's coming back from the server, we can tidy this up a little
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Netty AdminNetwork Administrator

Author

Commented:
Excellent, yes, I have it sending back https://www.google.com now so that we can move forward with the iFrame.
Netty AdminNetwork Administrator

Author

Commented:
Then we he gets the correct URL coming back we should be good to finish it up
Netty AdminNetwork Administrator

Author

Commented:

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.