Skip to content

Instantly share code, notes, and snippets.

@franz-josef-kaiser
Last active May 22, 2022 14:57
Show Gist options
  • Select an option

  • Save franz-josef-kaiser/4599369 to your computer and use it in GitHub Desktop.

Select an option

Save franz-josef-kaiser/4599369 to your computer and use it in GitHub Desktop.

Revisions

  1. franz-josef-kaiser revised this gist Mar 5, 2014. 1 changed file with 36 additions and 36 deletions.
    72 changes: 36 additions & 36 deletions curl_dump.php
    Original file line number Diff line number Diff line change
    @@ -24,8 +24,8 @@ public static function init()
    )
    return;

    null === self :: $instance AND self :: $instance = new self;
    return self :: $instance;
    null === self::$instance AND self::$instance = new self;
    return self::$instance;
    }

    public function __construct()
    @@ -34,9 +34,9 @@ public function __construct()
    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
    'http_request_redirection_count',
    array( $this, 'http_request_redirection_count' ),
    999
    );
    add_filter( 'https_ssl_verify', array( $this, 'https_ssl_verify' ), 999 );

    @@ -49,14 +49,14 @@ public function __construct()
    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' => ''
    )
    'id' => get_class( $this ),
    'parent' => false,
    'title' => 'cURL Debug',
    'href' => '#',
    'meta' => array(
    'class' => 'foo',
    'html' => '',
    ),
    ) );
    }

    @@ -69,30 +69,30 @@ public function add_node()
    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
    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 )
    '%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 )
    '<br />Error Code: %s<br />Error Message: %s',
    curl_errno( $handle ),
    curl_error( $handle )
    );
    $this->add_dump( $data );
    }
    @@ -147,7 +147,7 @@ public function dump_curl_buffer_cb( $curl, $data = null )
    */
    public function add_dump( $data )
    {
    self :: $dump[] = $data;
    self::$dump[] = $data;
    }

    /**
    @@ -157,9 +157,9 @@ public function add_dump( $data )
    */
    public function do_dump()
    {
    ! empty( self :: $dump ) AND printf(
    '<pre>%s</pre>'
    ,var_export( implode( "<br />", self :: $dump ), true )
    ! empty( self::$dump ) AND printf(
    '<pre>%s</pre>',
    var_export( implode( "<br />", self :: $dump ), true )
    );
    }
    }
  2. franz-josef-kaiser revised this gist Jan 24, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion curl_dump.php
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@
    add_action( 'plugins_loaded', array( 'WPSE81791_cURL', 'init' ) );
    class WPSE81791_cURL
    {
    protected static $instance;
    protected static $instance;

    public $time_out;
    public $redirections;
  3. franz-josef-kaiser revised this gist Jan 24, 2013. 1 changed file with 156 additions and 78 deletions.
    234 changes: 156 additions & 78 deletions curl_dump.php
    Original file line number Diff line number Diff line change
    @@ -1,87 +1,165 @@
    <?php
    /**
    * Plugin Name: (#81791) Dump cURL Request & Response
    /**
    * 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 static $dump = array();

    public static function init()
    {
    null === self :: $instance AND self :: $instance = new self;
    return self :: $instance;
    }

    public function __construct()
    {
    add_action( 'http_api_curl', array( $this, 'dump_curl' ) );
    add_action( 'shutdown', array( $this, 'do_dump' ) );
    }

    /**
    * 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( $handle, CURLINFO_HEADER_OUT, 1 );
    curl_setopt( $handle, CURLOPT_HEADER, 0 );
    curl_setopt( $handle, CURLOPT_HEADERFUNCTION, array( $this, 'dump_curl_buffer_cb' ) );
    curl_setopt( $handle, CURLOPT_WRITEFUNCTION, array( $this, 'dump_curl_buffer_cb' ) );
    curl_exec( $handle );
    $this->add_dump(
    curl_getinfo( $handle, CURLINFO_HEADER_OUT )
    .$this->dump_curl_buffer_cb( null )
    .'<br />Nr. of Errors: '.curl_errno( $handle )
    .'<br />Errors: '.curl_error( $handle )
    );
    }

    /**
    * 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 )
    );
    }
    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 )
    );
    }
    }
  4. franz-josef-kaiser revised this gist Jan 23, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions curl_dump.php
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ class WPSE81791_cURL
    {
    protected static $instance;

    public static $dump;
    public static $dump = array();

    public static function init()
    {
    @@ -79,7 +79,7 @@ public function add_dump( $data )
    */
    public function do_dump()
    {
    printf(
    ! empty( self :: $dump ) AND printf(
    '<pre>%s</pre>'
    ,var_export( implode( "<br />", self :: $dump ), true )
    );
  5. franz-josef-kaiser created this gist Jan 22, 2013.
    87 changes: 87 additions & 0 deletions curl_dump.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,87 @@
    <?php
    /**
    * Plugin Name: (#81791) Dump cURL Request & Response
    * Author: Franz Josef Kaiser
    */
    add_action( 'plugins_loaded', array( 'WPSE81791_cURL', 'init' ) );
    class WPSE81791_cURL
    {
    protected static $instance;

    public static $dump;

    public static function init()
    {
    null === self :: $instance AND self :: $instance = new self;
    return self :: $instance;
    }

    public function __construct()
    {
    add_action( 'http_api_curl', array( $this, 'dump_curl' ) );
    add_action( 'shutdown', array( $this, 'do_dump' ) );
    }

    /**
    * 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( $handle, CURLINFO_HEADER_OUT, 1 );
    curl_setopt( $handle, CURLOPT_HEADER, 0 );
    curl_setopt( $handle, CURLOPT_HEADERFUNCTION, array( $this, 'dump_curl_buffer_cb' ) );
    curl_setopt( $handle, CURLOPT_WRITEFUNCTION, array( $this, 'dump_curl_buffer_cb' ) );
    curl_exec( $handle );
    $this->add_dump(
    curl_getinfo( $handle, CURLINFO_HEADER_OUT )
    .$this->dump_curl_buffer_cb( null )
    .'<br />Nr. of Errors: '.curl_errno( $handle )
    .'<br />Errors: '.curl_error( $handle )
    );
    }

    /**
    * 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()
    {
    printf(
    '<pre>%s</pre>'
    ,var_export( implode( "<br />", self :: $dump ), true )
    );
    }
    }