My curl seems not properly

Discussion in 'PHP' started by Adulu, Mar 16, 2011.

  1. #1
    ob_start ();
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://$url?a=$a&b=$b");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_exec($ch);
    curl_close($ch);
    $data = ob_get_contents ();
    ob_end_clean ();
    ------------
    it seems take long time to run this scripts.
    or can't release memory or close it.

    someone can help me?, i need right properly curl code

    thank you
     
    Adulu, Mar 16, 2011 IP
  2. JoelLarson

    JoelLarson Peon

    Messages:
    61
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <?php
    
    $ch = curl_init();
    $options = array(
    CURLOPT_URL => "http://{$url}?a={$a}&b={$b}",
    CURLOPT_HEADER => 0,
    CURLOPT_CONNECTTIMEOUT => 5,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_SSL_VERIFYPEER => FALSE,
    CURLOPT_SSL_VERIFYHOST => FALSE,
    );
    
    curl_setopt_array($options);
    $data = curl_exec($ch);
    curl_close($ch);
    PHP:
     
    JoelLarson, Mar 16, 2011 IP
    Adulu likes this.
  3. Adulu

    Adulu Peon

    Messages:
    2,791
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks so much.

    beside, if it's a pure url.
    example:http://mydomain.com/abc/123.php

    is below code that are right?
    $ch = curl_init();
    CURLOPT_URL => "http://mydomain.com/abc/123.php",
    CURLOPT_HEADER => 0,
    CURLOPT_CONNECTTIMEOUT => 5,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_SSL_VERIFYPEER => FALSE,
    CURLOPT_SSL_VERIFYHOST => FALSE,
    curl_setopt_array($options);
    $data = curl_exec($ch);
    curl_close($ch);
    echo "$data";
    PHP:
    thanks
     
    Adulu, Mar 17, 2011 IP
  4. JoelLarson

    JoelLarson Peon

    Messages:
    61
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Unfortunately not. You've left out the array function :(

    <?php
    
    // Start the CURL handle.
    $ch = curl_init();
    
    // Define the options for CURL in an array.
    $options = array(
    // Set the URL to fetch.
    CURLOPT_URL => "http://mydomain.com/abc/123.php",
    // Don't return the headers.
    CURLOPT_HEADER => 0,
    // Set the page fetch timeout to 5 seconds.
    CURLOPT_CONNECTTIMEOUT => 5,
    // Return the data (usually html or xml)
    CURLOPT_RETURNTRANSFER => TRUE,
    // Don't verify the certificate (helps with retrieving non ssl sites.
    CURLOPT_SSL_VERIFYPEER => FALSE,
    // Don't verify the certificate host
    CURLOPT_SSL_VERIFYHOST => FALSE,
    );
    
    // Set the CURL options from the array.
    curl_setopt_array($options);
    
    // Execute the request and store it in a variable.
    $data = curl_exec($ch);
    
    // Close the CURL handle.
    curl_close($ch);
    
    // Echo the CURL's returned data.
    echo "$data";
    PHP:

    Here is the uncommented version:
    $ch = curl_init();
    $options = array(
    CURLOPT_URL => "http://mydomain.com/abc/123.php",
    CURLOPT_HEADER => 0,
    CURLOPT_CONNECTTIMEOUT => 5,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_SSL_VERIFYPEER => FALSE,
    CURLOPT_SSL_VERIFYHOST => FALSE,
    );
    curl_setopt_array($options);
    $data = curl_exec($ch);
    curl_close($ch);
    echo "$data";
    PHP:
     
    JoelLarson, Mar 17, 2011 IP