Php curl支持

时间:2012-10-25 08:27:25

标签: php curl

尝试了很多方法之后,你能帮我解决这个问题。

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

提前致谢!

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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');