Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active April 5, 2017 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/833c1d1819b8863697aea00a716b2116 to your computer and use it in GitHub Desktop.
Save tommcfarlin/833c1d1819b8863697aea00a716b2116 to your computer and use it in GitHub Desktop.
[WordPress] What Does It Mean to Use WordPress as a Proxy?
<?php
add_action( 'wp_ajax_get_all_data', 'get_all_data' );
/**
* Retrieves the requested data from the specified URL
* returns the values in JSON.
*/
function get_all_data() {
// TODO
}
var _get_columns = function() {
// Don't forget to use a nonce or security value here.
var data = {
'action': 'get_all_columns'
'security': '<?php echo wp_create_nonce( "acme-security-ajax-nonce" ); ?>'
};
// TODO Check for error logging if the response value doesn't exist.
$.get( ajaxurl, data, function( response ) {
response = $.parseJSON( response );
// Your custom functionality here
});
};
<?php
add_action( 'wp_ajax_get_all_data', 'get_all_data' );
/**
* Retrieves the requested data from the specified URL
* returns the values in JSON.
*/
function get_all_data() {
// Note $url is up to you.
$curl = curl_init( $url );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY );
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
$response = curl_exec( $curl );
$resultStatus = curl_getinfo( $curl );
if( 200 !== $resultStatus['http_code'] ) {
echo 'Call Failed '.print_r( $result_status );
}
curl_close( $curl );
echo json_encode( utf8_encode( $response ) );
wp_die();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment