简体   繁体   English

帮助编码 cURL 参数(从命令行可以正常工作,但不能从 PHP 脚本)

[英]Help encoding a cURL argument (works fine from the command line, but not from a PHP script)

I am working with the Facebook API, and successfully use the following command via terminal to post a message to another users wall.我正在使用 Facebook API,并通过终端成功使用以下命令将消息发布到另一个用户墙。

curl -F 'access_token=XXXXXXXXXX' \
 -F 'message=Hello World' \
 -F 'to={["id":XXXXXXX]}' \
 https://graph.facebook.com/me/feed

This works great.这很好用。 I am trying to do the same via php with this code;我正在尝试使用此代码通过 php 做同样的事情;

$fields = array(
    'access_token' => $t,
    'message' => $message,
    'to' => '{["id":'.$id.']}'
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,  $url);
curl_setopt($ch, CURLOPT_POST, 1);          
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_exec($ch);
curl_close($ch);

This code successfuly posts a message, but it does it to my OWN wall (ie it is ignoring the 'to' parameter).此代码成功地发布了一条消息,但它在我的 OWN 墙上执行(即它忽略了“to”参数)。 I'm new to cURL, and I'm sure I am encoding it wrong, or maybe missing a cURL flag, but I've been through several tutorials on POSTing via cURL, including a few SO answers, and I can't see what I'm missing.我是 cURL 的新手,我确定我编码错误,或者可能缺少 cURL 标志,但我已经通过 ZC1BEF293167E5FE84BD35C7D87D3E 发布了几个关于通过 ZC1BEF293167E5FE84BD35C7D87D3E 发布的教程,包括我错过了什么。

Really appreciate any help!非常感谢任何帮助!

What does this print out?这打印出什么?

if ( 'POST' == $_SERVER[ 'REQUEST_METHOD' ]) {
    echo 'Posted: ';
    print_r( $_POST );
    exit;   
}


$t = '121';
$message = 'helo Worlds';
$id = 1234;

$fields = array(
    'access_token' => $t,
    'message' => $message,
    'to' => '{["id":'.$id.']}'
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,  'http://localhost:8888/testbed/' ); // THIS script
curl_setopt($ch, CURLOPT_POST, 1);          
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE );

$out = curl_exec($ch);
curl_close($ch);

echo 'Received[ ' . $out . ' ]';

Prints this on my local box:在我的本地盒子上打印:

Received[ Posted: Array ( [access_token] => 121 [message] => helo Worlds [to] => {[\"id\":1234]} ) ]

UPDATED:更新:

$fields should be a GET like string $fields 应该是一个类似 GET 的字符串

  para1=val1&para2=val2&...

or an array:或数组:

The full data to post in a HTTP "POST" operation.在 HTTP“POST”操作中发布的完整数据。 To post a file, prepend a filename with @ and use the full path.要发布文件,请在文件名前加上 @ 并使用完整路径。 The filetype can be explicitly specified by following the filename with the type in the format ';type=mimetype'.文件类型可以通过使用格式为“;type=mimetype”的文件名来明确指定。 This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value .此参数可以作为 urlencoded 字符串(如 'para1=val1&para2=val2&...' )或作为字段名称作为键和字段数据作为值的数组传递。 If value is an array, the Content-Type header will be set to multipart/form-data.如果 value 是一个数组,则 Content-Type header 将设置为 multipart/form-data。 As of PHP 5.2.0, files thats passed to this option with the @ prefix must be in array form to work.从 PHP 5.2.0 开始,使用 @ 前缀传递给此选项的文件必须采用数组形式才能工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM