Parameter passing to CURL GET Request



This is simple example to pass parameters to php curl get method

<?php

    /* Script URL */
    $url = 'http://www.example.com/abc.php';

    /* $_GET Parameters to Send */
    $params = array('param1' => 'value1', 'param2' => 'value2');

    /* Update URL to container Query String of Paramaters */
    $url .= '?' . http_build_query($params);

    /* cURL Resource */
    $ch = curl_init();

    /* Set URL */
    curl_setopt($ch, CURLOPT_URL, $url);

    /* Tell cURL to return the output */
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    /* Tell cURL NOT to return the headers */
    curl_setopt($ch, CURLOPT_HEADER, false);

    /* Execute cURL, Return Data */
    $data = curl_exec($ch);

    /* Check HTTP Code */
    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    /* Close cURL Resource */
    curl_close($ch);

    /* 200 Response! */
    if ($status == 200) {

        /* Debug */
        var_dump($data);

    } else {

        /* Debug */
        var_dump($data);
        var_dump($status);

    }

?>

0 comments :