Search code examples
phpcurlget

Trying to use curl to do a GET, value being sent is allows null


I'm trying to use curl to do a simple GET with one parameter called redirect_uri. The php file that gets called prints out a empty string for $_GET["redirect_uri"] it shows red= and it seems like nothing is being sent. code to do the get

//Get code from login and display it
$ch = curl_init();

$url = 'http://www.besttechsolutions.biz/projects/facebook/testget.php';

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_GET,1);
curl_setopt($ch,CURLOPT_GETFIELDS,"redirect_uri=my return url");

//execute post
 print "new reply 2 <br>";
 $result = curl_exec($ch);
 print $result;
// print "<br> <br>";
// print $fields_string;
 die("hello");

the testget.php file

<?php
print "red-";
print $_GET["redirect_uri"];


?>

Solution

  • This is how I usually do get requests, hopefully it will help you:

    // create curl resource
    $ch = curl_init();
    
    //return the transfer as a string
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    // Follow redirects
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    
    // Set maximum redirects
    curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
    
    // Allow a max of 5 seconds.
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    
    // set url
    if( count($params) > 0 ) {
        $query = http_build_query($params);
        curl_setopt($ch, CURLOPT_URL, "$url?$query");
    } else {
        curl_setopt($ch, CURLOPT_URL, $url);
    }
    
    // $output contains the output string
    $output = curl_exec($ch);
    
    // Check for errors and such.
    $info = curl_getinfo($ch);
    $errno = curl_errno($ch);
    if( $output === false || $errno != 0 ) {
        // Do error checking
    } else if($info['http_code'] != 200) {
        // Got a non-200 error code.
        // Do more error checking
    }
    
    // close curl resource to free up system resources
    curl_close($ch);
    
    return $output;
    

    In this code, the $params could be an array where the key is the name, and the value is the value.