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

99封情书 提交于 2019-12-10 15:59:58

问题


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"];


?>

回答1:


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.



来源:https://stackoverflow.com/questions/7516863/trying-to-use-curl-to-do-a-get-value-being-sent-is-allows-null

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!