Php 使用curl在url中传递变量

Php 使用curl在url中传递变量,php,facebook-graph-api,file-get-contents,Php,Facebook Graph Api,File Get Contents,我想将变量传递给curl url,但我得到了一个错误。 帮帮我 代码是: $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_handl

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

代码是:

$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);

从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);

您可以在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);
shuffle()
返回一个布尔值。所以你最终会得到一个正确或错误的答案。不要将其分配给
$messages

就这么做吧

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

你能发布你的代码和错误吗?$arr=file('comments.txt',file\u IGNORE\u NEW\u line)$消息=shuffle($arr);回音信息$id=修剪($_POST['POST_id'])$url=''$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\u exec($curl\u handle);卷曲关闭($curl\u handle);你能把它贴在你的问题上吗?对不起,我没听懂你的错误!我无法设置。我能把文件寄给你吗?你能做到吗?检查这个对不起!只要写下你的$message和$data数组,并把它给我就行了!c它是有效的。。但是现在数组出现了问题,messege数组无法获得随机注释$arr=file('comments.txt',file\u IGNORE\u NEW\u line)$消息=shuffle($arr);好的,我很抱歉,我正在打印阵列message@user3563010,我修改了代码。。你现在可以试试吗?兄弟,所有设置的方法和accesstoken。。现在给我comments.txt的代码,用于保存变量$messages谢谢你发布你的
comments.txt
?完成了,兄弟!我现在得到输出了!非常感谢你!
$arr = file('comments.txt',FILE_IGNORE_NEW_LINES);
shuffle($arr);
$data = array("message"=>implode('',$arr),
"method"=>"post",
"access_token"=>$m
);