Skip to content

Instantly share code, notes, and snippets.

@warunanc
Forked from franz-josef-kaiser/curl_dump.php
Created March 30, 2018 09:03
Show Gist options
  • Save warunanc/545a95409ed2320cffdc3d2625182c53 to your computer and use it in GitHub Desktop.
Save warunanc/545a95409ed2320cffdc3d2625182c53 to your computer and use it in GitHub Desktop.
Debug and dump a WordPress cURL Request and it's response during a WP HTTP API call.
<?php
/**
* Plugin Name: Dump WP HTTP API cURL Request & Response
* Author: Franz Josef Kaiser
*/
add_action( 'plugins_loaded', array( 'WPSE81791_cURL', 'init' ) );
class WPSE81791_cURL
{
protected static $instance;
public $time_out;
public $redirections;
public $version;
public $is_ssl;
public static $dump = array();
public static function init()
{
if (
defined( 'XMLRPC_REQUEST' )
OR defined( 'DOING_AJAX' )
OR defined( 'IFRAME_REQUEST' )
)
return;
null === self::$instance AND self::$instance = new self;
return self::$instance;
}
public function __construct()
{
// Retrieve filtered vars
add_filter( 'http_request_timeout', array( $this, 'http_request_timeout' ), 999 );
add_filter( 'http_request_version', array( $this, 'http_request_version' ), 999 );
add_filter(
'http_request_redirection_count',
array( $this, 'http_request_redirection_count' ),
999
);
add_filter( 'https_ssl_verify', array( $this, 'https_ssl_verify' ), 999 );
add_action( 'admin_bar_menu', array( $this, 'add_node' ), 120 );
add_action( 'http_api_curl', array( $this, 'dump_curl' ) );
add_action( 'shutdown', array( $this, 'do_dump' ) );
}
public function add_node()
{
is_admin_bar_showing() AND $GLOBALS['wp_admin_bar']->add_node( array(
'id' => get_class( $this ),
'parent' => false,
'title' => 'cURL Debug',
'href' => '#',
'meta' => array(
'class' => 'foo',
'html' => '',
),
) );
}
/**
* Debug the response in the middle.
* Catches the cURL object during the request.
* @param cURL $handle
* @return void
*/
public function dump_curl( &$handle )
{
curl_setopt_array( $handle, array(
CURLINFO_HEADER_OUT => true,
CURLOPT_HEADER => true,
CURLOPT_HEADERFUNCTION => array( $this, 'dump_curl_buffer_cb' ),
CURLOPT_WRITEFUNCTION => array( $this, 'dump_curl_buffer_cb' ),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CERTINFO => true,
CURLOPT_FOLLOWLOCATION => true,
#CURLOPT_NOPROGRESS => true,
#CURLOPT_CONNECTTIMEOUT => $this->time_out,
#CURLOPT_TIMEOUT => $this->time_out,
#CURLOPT_HTTP_VERSION => $this->version,
#CURLOPT_MAXREDIRS => $this->redirections,
#CURLOPT_SSL_VERIFYHOST => $this->is_ssl,
) );
curl_exec( $handle );
$data = sprintf(
'%s<br />%s',
curl_getinfo( $handle, CURLINFO_HEADER_OUT ),
$this->dump_curl_buffer_cb( null )
);
0 != curl_errno( $ch ) AND $data .= sprintf(
'<br />Error Code: %s<br />Error Message: %s',
curl_errno( $handle ),
curl_error( $handle )
);
$this->add_dump( $data );
}
public function http_request_timeout( $sec )
{
$this->time_out = $sec;
return $sec;
}
public function http_request_redirection_count( $times )
{
$this->redirections = $times;
return $times;
}
public function http_request_version( $version )
{
$this->version = $version;
return $version;
}
public function https_ssl_verify( $is_ssl )
{
$this->is_ssl = $is_ssl;
return $is_ssl;
}
/**
* Callback for cURL dump method
* @param object $curl
* @param null $data
* @return int
*/
public function dump_curl_buffer_cb( $curl, $data = null )
{
static $buffer = '';
if ( is_null( $curl ) )
{
$r = $buffer;
$buffer = '';
return $r;
}
$buffer .= $data;
return strlen( $data );
}
/**
* Adds data to the static data stack
* @param
* @return void
*/
public function add_dump( $data )
{
self::$dump[] = $data;
}
/**
* Dumps the data stack for debug
* @param
* @return void
*/
public function do_dump()
{
! empty( self::$dump ) AND printf(
'<pre>%s</pre>',
var_export( implode( "<br />", self :: $dump ), true )
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment