2012-10-25 127 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