使用curl在url中传递变量

时间:2014-04-23 06:09:11

标签: php facebook-graph-api file-get-contents

我想将变量传递给curl url,但是我收到了一个错误。 帮助我

    $url = 'https://graph.facebook.com/$id/comments?message=$messages&method=post&access_token=$m';

代码是:

$arr = file('comments.txt',FILE_IGNORE_NEW_LINES); $messages = shuffle($arr); 
echo messages; 
$id = trim($_POST['post_id']); 
$url = 'graph.facebook.com/$id/…;; 
$curl_handle=curl_init(); 
curl_setopt($curl_handle,CURLOPT_URL, '$url'); curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,200); curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1); 
$send = curl_exec($curl_handle); curl_close($curl_handle);

3 个答案:

答案 0 :(得分:2)

从URL中删除参数并编写以下行以传递CURL中的参数

curl_setopt($s,CURLOPT_POST,true);
curl_setopt($s,CURLOPT_POSTFIELDS,'your post parameters');

作为您的示例,这是代码。

$arr = file('comments.txt',FILE_IGNORE_NEW_LINES);
$messages = shuffle($arr);
echo messages;
$data = array("message"=>$messages,
"method"=>"post",
"access_token"=>$m
);


$id = trim($_POST['post_id']);
$url = 'https://graph.facebook.com/$id/comments';
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL, $url);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,200);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
curl_setopt($s,CURLOPT_POST,true);

curl_setopt($s,CURLOPT_POSTFIELDS, http_build_query($data));
$send = curl_exec($curl_handle);
curl_close($curl_handle);

答案 1 :(得分:2)

您可以在CURLOPT_POSTFIELDS中传递所有变量,请参阅以下代码:

$ch = curl_init();                  
$url = "https://graph.facebook.com/$id/comments"; 
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, "message=some_msg&method=post&access_token=xyz"); // define what you want to post
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$output = curl_exec ($ch);
curl_close ($ch); 
var_dump($output);

答案 2 :(得分:1)

shuffle()返回一个布尔值。所以你最终会得到真或假。不要将其分配给$messages

只需这样做..

$arr = file('comments.txt',FILE_IGNORE_NEW_LINES);
shuffle($arr);
$data = array("message"=>implode('',$arr),
"method"=>"post",
"access_token"=>$m
);