JPNY
JPNY

Reputation: 21

Trying to debug PHP CURL using CURLOPT_VERBOSE

I'm trying to debug my PHP CURL. I cant figure out why $debug is always empty.

I tried $out = fopen('php://temp', 'w+'); and $out = fopen('php://output', 'w'); and fopen('php://memory', 'w+');

ob_start();  
  $out = fopen('php://temp', 'w+');

  $ch = curl_init();
 
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
  curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
  curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:50.0) Gecko/20100101 Firefox/50.0');
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,1);
  curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  curl_setopt($ch, CURLOPT_VERBOSE, true);
  curl_setopt($ch, CURLOPT_STDERR, $out);   
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: '.$domain));
  curl_setopt($ch, CURLOPT_URL, $url);

  $data = curl_exec($ch);


  curl_close($ch);

  fclose($out);  
  $debug = ob_get_clean();


  echo "<script>console.log($debug );</script>";

Upvotes: 1

Views: 2321

Answers (1)

Professor Abronsius
Professor Abronsius

Reputation: 33804

There is no need to use output buffering to use the curl function to obtain verbose debug information. You do however need to make a couple of alterations to the curl config and also process the response data in a way that will no cause the Javascript engine to baulk at the raw PHP debug info in the response.

<?php

    $url='https://www.forfarathletic.co.uk/';
    $domain='forfarathletic.co.uk';
    
    $out = fopen('php://temp', 'w+');
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,1);
    curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Host: ' . $domain ));
    
    # This is where the settings to use verbose debugging are set
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_NOPROGRESS, true);
    curl_setopt($ch, CURLOPT_STDERR, $out);


    $data=(object)array(
        'response'  =>  curl_exec( $ch ),
        'status'    =>  curl_getinfo( $ch, CURLINFO_RESPONSE_CODE )
    );
    
    # To utilise the recorded verbose data rewind to the beginning and read the stream contents.
    rewind( $out );
    $data->verbose=stream_get_contents( $out );
    
    
    curl_close( $ch );
    fclose( $out );
        
    
    
    
    # to display the data in the console you need to make it Javascript friendly.
    # This pattern & replacement will replace newline characters to make it suitable for javascript multiline display.
    $pttn='@(\r\n|\n)@i';
    $repl='"+\'\n\'+"';
    
    
    # We need to also remove double quotes from outpt response - replace with single quotes before calling preg_replace.
    $status=$data->status;
    $verbose=str_replace('"',"'", $data->verbose );
    

    printf('<script>console.info("Status code:%s\n\nVerbose output:\n%s")</script>', $status, preg_replace(
        $pttn,
        $repl,
        $verbose
    ));
?>

The above will yield a console output like this:

Verbose output:
*   Trying 198.244.241.9...
* TCP_NODELAY set
* Connected to www.forfarathletic.co.uk (198.244.241.9) port 443 (#0)
* ALPN, offering http/1.1
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server accepted to use http/1.1
* Server certificate:
*  subject: CN=forfarathletic.co.uk
*  start date: Jun 26 22:27:10 2022 GMT
*  expire date: Sep 24 22:27:09 2022 GMT
*  issuer: C=US; O=Let's Encrypt; CN=R3
*  SSL certificate verify result: unable to get local issuer certificate (20), continuing anyway.
> GET / HTTP/1.1
Host: forfarathletic.co.uk
User-Agent: Mozilla/5.0
Accept: */*

< HTTP/1.1 200 OK
< Server: nginx
< Date: Wed, 06 Jul 2022 07:23:10 GMT
< Content-Type: text/html; charset=utf-8
< Content-Length: 61177
< Connection: keep-alive
< X-Powered-By: PHP/8.0.20
< Pragma: no-cache
< Expires: Wed, 17 Aug 2005 00:00:00 GMT
< Cache-Control: private, max-age=300, no-store, no-cache, must-revalidate, post-check=0, pre-check=0
< Set-Cookie: 8ee46b7d8b12b45bd31527db2ecf1085=pc52ai8q48tith3n7a3uglvui8; path=/; HttpOnly
< Set-Cookie: fc_uid=p
< Last-Modified: Wed, 06 Jul 2022 07:23:10 GMT
< Vary: Accept-Encoding
< X-Powered-By: PleskLin
< 
* Connection #0 to host www.forfarathletic.co.uk left intact

Upvotes: 2

Related Questions