0

在尝试了很多方法之后,你能帮我修复这个代码吗?

总是返回{"code":201,"error":"missing user password"}

<?php 
$APPLICATION_ID = "XXXXXXXXXXXXXx"; 
$REST_API_KEY = "XXXXXXXXXXXX"; 
$url = 'https://api.parse.com/1/login';

$headers = array(
'X-Parse-Application-Id: ' . $APPLICATION_ID,
'X-Parse-REST-API-Key: ' . $REST_API_KEY,
//option 1
//urlencode('username=xxxxxx'),
//urlencode('password=xxxxxxx'),
);

$curl = curl_init($url); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl, CURLOPT_TIMEOUT, '3'); 
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 
//option 2 
//curl_setopt($curl, CURLOPT_ENCODING, 'username=xxxxx'); 
//curl_setopt($curl, CURLOPT_ENCODING, 'password=xxxxxxx');

$content = curl_exec($curl); curl_close($curl);

print $content

?>

是使用 PHP 登录

提前致谢!

4

2 回答 2

0

正如 Kenny 指出的那样,您需要以下列格式发送您的 POST 变量。我喜欢为此使用http_build_query :

$postData = array(
    'username' => 'Test',
    'password' => 'Tester'
);

curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postData));

如果您正在处理 HTTP 身份验证:

curl_setopt($curl, CURLOPT_USERPWD, 'username:password');
于 2012-10-25T08:40:45.343 回答
0

您需要按以下方式传递此数据:

$data = array(
   'username' => urlencode('XXXXX'),
   'password' => urlencode('XXXXXXX')
);

// URL-IFY
foreach($data as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

然后按如下方式传递数据:

curl_setopt($curl,CURLOPT_POST, count($fields));
curl_setopt($curl,CURLOPT_POSTFIELDS, $fields_string);
于 2012-10-25T08:33:29.453 回答