skip to Main Content

I am trying to convert this CURL command:

curl -i https://myurl >toto.txt

into a PHP script, but it doesn’t work.

$ch_rech = curl_init();
curl_setopt($ch_rech, CURLOPT_URL, "https://myurl");  
curl_setopt($ch_rech, CURLOPT_HEADER, 0);  
ob_start();                                
curl_exec($ch_rech);                       
curl_close($ch_rech);                      
$Results = ob_get_contents();              
ob_end_clean();

How can I do it?

3

Answers


  1. Chosen as BEST ANSWER

    It's good, thank you for all. The problem was that my web server is blacklisted by the destionation server ;o)


  2. Try so:

    $ch_rech = curl_init();
    curl_setopt($ch_rech, CURLOPT_URL, 'https://myurl');  
    curl_setopt($ch_rech, CURLOPT_HEADER, false);  
    curl_setopt($ch_rech, CURLOPT_RETURNTRANSFER, true);
    
    $resp = curl_exec ($ch_rech);
    $httpStatus = curl_getinfo($ch_rech, CURLINFO_HTTP_CODE);
    curl_close ($ch_rech);   
    

    $resp contains the output (or false if there is an error)
    $httpStatus contains (obviously 🙂 ) the HTTP status code returned

    Login or Signup to reply.
  3. The question suggests that you wish to write the response of the curl request to a text file yet the comment suggests totally the opposite. The URL you are using is https so one has to usually supply a few more parameters to the curl request, such as the public SSL certificate info for which you can download a copy of cacert.pem

    To write the output to a file:

    <?php
    
        # use a path that is appropriate to your webserver & site setup
        $fh=fopen('toto.txt','w');
        $cacert='c:/wwwroot/cacert.pem';
    
        $curl=curl_init('https://myurl');
        curl_setopt( $curl, CURLOPT_HEADER, 0 );
        curl_setopt( $curl, CURLOPT_FILE, $fh );  
        curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, true );
        curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
        curl_setopt( $curl, CURLOPT_CAINFO, $cacert );
        curl_setopt( $curl, CURLOPT_USERAGENT, 'Mozilla/5.0' );
        curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
        curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
        curl_exec( $curl );
        curl_close( $curl );
        fclose( $fh );
    
    ?>
    

    To display the response directly:

    <?php
    
        # use a path that is appropriate to your webserver & site setup
        $cacert='c:/wwwroot/cacert.pem';
    
        $curl=curl_init('https://myurl');
        curl_setopt( $curl, CURLOPT_HEADER, 0 );
        curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, true );
        curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
        curl_setopt( $curl, CURLOPT_CAINFO, $cacert );
        curl_setopt( $curl, CURLOPT_USERAGENT, 'Mozilla/5.0' );
        curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
        curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
        $res=curl_exec( $curl );
        $status=curl_getinfo( $curl, CURLINFO_RESPONSE_CODE );
        curl_close( $curl );
        
        if( $status==200 ){
            echo $res;
        }
    ?>
    

    CURLOPT_FOLLOWLOCATION – useful if the server sends location headers ( if the original url has been changed for example )

    TRUE to follow any "Location: " header that the server sends as part
    of the HTTP header (note this is recursive, PHP will follow as many
    "Location: " headers that it is sent, unless CURLOPT_MAXREDIRS is
    set).

    CURLOPT_RETURNTRANSFER – when you want to capture the response from the request into a variable.

    TRUE to return the transfer as a string of the return value of
    curl_exec() instead of outputting it directly.

    In the latter example above by setting CURLOPT_RETURNTRANSFER and capturing the http response code by using $status=curl_getinfo( $curl, CURLINFO_RESPONSE_CODE ); we can fork any logic needed to display/parse/scrape content

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search