【问题标题】:Trying to use curl to do a GET, value being sent is allows null尝试使用 curl 进行 GET,发送的值允许为 null
【发布时间】:2011-09-22 14:53:48
【问题描述】:

我正在尝试使用 curl 执行一个简单的 GET,其中包含一个名为 redirect_uri 的参数。被调用的 php 文件为 $_GET["redirect_uri"] 打印出一个空字符串,它显示 red= 并且似乎没有发送任何内容。 获取代码

//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 应该是/做什么?

标签: php curl get


【解决方案1】:

这就是我通常接收请求的方式,希望对您有所帮助:

// 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 文件时工作,但在调用 Facebook url 时 djd 不工作。如果我将 url 粘贴到浏览器中,facebook api 会返回正确的信息,这是没有希望的,但感谢 Ted 的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-08
  • 2018-03-09
  • 2017-01-11
  • 2012-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多