public
Last active

Debug and dump a WordPress cURL Request and it's response during a WP HTTP API call.

  • Download Gist
curl_dump.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
<?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 )
);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.