2012-10-25 86 views
0

尝试了很多方法后,你能帮我解决这个问题。Php卷曲支持

始终返回{"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

感谢事先登录!

+0

哪里是API的规格?该API接受哪些内容类型? – hakre

回答

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

正如肯尼指出的那样,你需要把你的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'); 
+0

201响应代码表明这不是HTTP身份验证(但这仅仅是我的一个假设,我已经在评论中询问了API规范)。 – hakre