我正在使用带有CPanel和MySQL数据库的Wordpress网站。我希望能够从Parse.com上的MongoDB中读取数据。最终,我想更改Wordpress的login.php脚本以搜索MongoDB并在必要时创建用户。
我在连接数据库时遇到很多麻烦。
这是我的PHP脚本:
<?php
$url = 'http://mercury.example.com:2234/parse/login';
$data = array('username' => 'username', 'password' => 'password');
$appID = "X-Parse-Application-Id: parseAppID";
$restKey = "X-Parse-REST-API-Key: praseRESTapiKey";
$session = "X-Parse-Revocable-Session: 1";
$contentType = "Content-Type: application/json";
$context = array(
'http'=> array(
"method" => "GET",
"header" => $appID . $restKey . $session . $contentType,
"content" => http_build_query($data)));
$context = stream_context_create($context);
$result = file_get_contents($url, false, $context);
var_dump($result);
?>
我收到的错误是:
Notice: file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded in C:\wamp\www\parseDB.php on line 26
Warning: file_get_contents(http://mercury.example.com:2234/parse/login): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in C:\wamp\www\parseDB.php on line 26
据我了解,403错误表示Web服务器正在返回“禁止”状态代码。
我正在使用WAMP在本地主机上测试我的php脚本。我的一位同事试图在Bash上执行类似的命令,并收到了回复。(我把它弄出来了,所以更容易阅读)。
curl -X GET
-H "X-Parse-Application-Id: parseAppID"
-H "X-Parse-REST-API-Key: parseRESTapiKey"
-H "X-Parse-Revocable-Session: 1"
-G --data-urlencode 'username=username' --data-urlencode 'password=password'
http://mercury.example.com:2234/parse/login
到目前为止,我已经坚持了2天,我不知道发生了什么。感谢您能获得的所有帮助。
编辑这是我的最终解决方案:
$url = 'http://mercury.example.com:3432/parse/login';
$data = array('username' => 'USERNAME', 'password' => 'PASSWORD!');
$context = array(
'http'=> array(
'method' => "GET",
'header' => "X-Parse-Application-Id: APPID\r\n" .
"X-Parse-REST-API-Key: RESTAPIKEY\r\n" .
"X-Parse-Revocable-Session: 1" .
"Content-Type: application/json\r\n",
'content' => http_build_query($data)
)
);
$context = stream_context_create($context);
$result = file_get_contents($url, false, $context);
var_dump($result);
?>
查看位于http://php.net/manual/en/function.stream-context-create.php的stream_content_create示例,该示例说明了如何正确传递标头。这时,您将每个标头猛击在一起而没有任何换行符,这会使它们看起来像串联的字符串:
X-Parse-Application-Id: parseAppIDX-Parse-REST-API-Key: praseRESTapiKeyX-Parse-Revocable-Session: 1Content-Type: application/json
提示-\r\n
在每个标题行之后添加。
除了file_get_contents,您还可以使用curl
方法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句