1

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>";
4
  • If you're trying to read the errors, you're writing them to php://temp Commented Jul 6, 2022 at 2:35
  • Does this answer your question? CURLOPT_VERBOSE not working Commented Jul 6, 2022 at 2:35
  • I saw that post. He is trying to write to a text file. I would prefer to write it to a string so I can output it to the browser console. If I cant find an answer to my issue I can always try that Commented Jul 6, 2022 at 3:07
  • 1
    But why are you trying to use output buffering, when you made cURL actually write the debug output to a file? You want to get the contents of that file, not of the output buffer that no one wrote anything into. Commented Jul 6, 2022 at 6:53

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

4 Comments

Glad it helped - perhaps mark the above?!
sorry to be dumb - how do I "mark" it? Im new to stackoverflow. I tried moving your answer up but it says I need a reputation of 15
really? not to worry but surprised that you cannot +1 or accept the answer. Anyway, good luck with the rest of your coding adventure
Why is debugging cURL so difficult??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.