使用curl进行GET请求,发送的值允许为空。

4

我想使用curl传递一个名为redirect_uri的参数进行简单的GET请求。被调用的PHP文件输出了$_GET["redirect_uri"]的空字符串,它显示为red=,似乎没有发送任何内容。

以下是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");

testget.php文件

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


?>

CURLOPT_GETFIELDS 是用来做什么的? - Daniel Stenberg
1个回答

11

这是我通常执行GET请求的方法,希望能够帮到你:

// 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;
在这段代码中,$params 可能是一个数组,其中键为名称,值为值。

谢谢,我尝试了这段代码,当它对我的PHP文件进行GET请求时可以工作,但是当调用Facebook的URL时却无法工作。如果我在浏览器中粘贴URL,Facebook API会返回正确的信息。这真是令人绝望,但还是感谢Ted的帮助。 - Ted pottel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接