RSS .92| RSS 2.0| ATOM 0.3
  • Home
  • About Me
  • My Publications
  • My Work Domain
  • phpResource Feeds

    cURL Callback Function

    Usually we get cURL response from curl_exec(). But we have options to get the output using callback functions. We can set callback function for both header and body output. Here is an example:

    //Init curl session
    $ch = curl_init();
    //Set URL
    curl_setopt($ch, CURLOPT_URL, ‘’);
    // Set callback function for headers
    curl_setopt($ch, CURLOPT_HEADERFUNCTION, ‘read_header’);
    // Set callback function for body
    curl_setopt($ch, CURLOPT_WRITEFUNCTION, ‘read_body’);
    //Execute curl

    //close curl session

    //Callback function for header
    function read_header($ch, $string)
    $length = strlen($string);
    echo “Header: $string
    return $length;

    //Callback function for body
    function read_body($ch, $string)
    $length = strlen($string);
    echo “Received $length bytes
    return $length;

    This script demonstrates how you can set callback functions to receive the HTTP response as it comes through. It’s really easy to use but nice feature.

    Enjoy PHPing !


    One Response to “cURL Callback Function”

    1. shimul says:

      nice example. thanks.

    Leave a Reply

    You must be logged in to post a comment.