Repository navigation
How to use parse-server with the PHP SDK ? #124
Description
Activity
This is without testing (as I haven't started migration testing yet), but I would imagine that it follows a similar line to the other platforms which would be to change the following in the ParseClient.php to point your server.
final class ParseClient { /** * Constant for the API Server Host Address. */ const HOST_NAME = 'https://api.parse.com';My bad, forgot to restart my Parse server after setting the new keys. Now, I'm getting a new error "Bad Request", but I'm working on fixing it
This is due to the default route for the parse-server residing on '/parse' where the Parse PHP SDK utilises '/1'. Refer to this line in ParseClient.php:24
const API_VERSION = '1';. Either change your route in your parse-server instantiation:// Mount the Parse API server middleware to /1 app.use('/1', parseServer);Or modify your ParseClient.php to use '/parse'.
I assume this will be fixed in the next release of the SDK.
I need to update the PHP SDK with a method for changing the server URL... I missed it.
@fraxool would you be willing to share your source code on how you got your swift code connected to your parse server?
Waiting for method to change server URL ... thanks !
Posted here this issue at PHP SDK.@gfosco any updates to the php sdk you can push please?
So I did the sort of hack today and changed some values in ParseClient.php
const HOST_NAME = 'https://xxxxxx.herokuapp.com';and
const API_VERSION = 'parse';My app key, rest key and master key all match up whats on the server, and I'm able to do a CURL request to get the data im trying to in php, but every time I try it in php it returns "unauthorized"
I have checked my keys and restarted my parse server to make sure nothing silly was happening and since the CURL request worked I'm not sure what to do next.. any ideas?
Heroku log
2016-02-09T21:25:52.623179+00:00 heroku[router]: at=info method=GET path="/parse/classes/UserTag?where=%7B%22gamelID%22%3A%22afcd60f2-f9b2-4c5f-9148-1de46713552b%22%7D" host=xxxxxx.herokuapp.com request_id=18d1c97b-6ffc-4942-ac16-b0b2c20a6504 fwd="173.230.155.65" dyno=web.1 connect=0ms service=6ms status=403 bytes=273any ideas?
Anyone have any ideas.. im pretty stuck from moving forward from my comment above ^^^
that hack enabled me to read values from my parse-server, but the save() function doesn't seem to work in the current php sdk...
@occurrentarts what did you change to make this work.. I'm currently stuck on the php side of things :(
- first i updated my parse php sdk to the newest
- then i opened ParseClient.php and set the hostname equal to my server:
const HOST_NAME = 'http://parseserver-bv###-env.elasticbeanstalk.com'; - then i changed the API_Version key like so:
const API_VERSION = 'parse'; - then i opened ParseFile.php and added my file key to the path ( line 227? )
$url = ParseClient::getAPIUrl().'files/my_file_key/'.$this->getName(); - then i was hitting a 413 error whenever i tried to upload content above 1mb, so i followed this process to expand the limit on my parse-server:
finally i have object saving and image and audio uploads working with the PHP SDK.
1 remaining item
thanks @gfosco
does it continue to require the fileKey or is that taken care of by parse-server?@occurrentarts fileKey should only be required on the parse-server side, for legacy files hosted by Parse.
@gfosco thanks for the update.. I'm getting an unauthorized response when doing a query.
Here is simple php script..
require 'autoload.php'; use Parse\ParseClient; $app_id = "X5hlsQoD2uY0Za2zQUyy"; $rest_key = "X3eX9XwzzJpkmMfjLR8E"; $master_key = "GhgoqbRnKkHN9ctsPAYu"; ParseClient::initialize( $app_id, $rest_key, $master_key); ParseClient::setServerURL('https://xxxxxx.herokuapp.com/parse'); use Parse\ParseObject; use Parse\ParseQuery; use Parse\ParseACL; use Parse\ParsePush; use Parse\ParseUser; use Parse\ParseInstallation; use Parse\ParseException; use Parse\ParseAnalytics; use Parse\ParseFile; use Parse\ParseCloud; try { $query = new ParseQuery("UserTag"); $query->equalTo("gameID", "241e84cf-055e-43b8-a5ad-75c7c87c75ea"); $results = $query->find(); echo "Successfully retrieved " . count($results) . " scores."; // Do something with the returned ParseObject values for ($i = 0; $i < count($results); $i++) { $object = $results[$i]; echo $object->getObjectId() . ' - ' . $object->get('factualID'); } } catch (\Exception $e){ echo $e->getMessage(); }Heroku logs..
at=info method=GET path="/parse/classes/UserTag?where=%7B%22gameID%22%3A%22241e84cf-055e-43b8-a5ad-75c7c87c75ea%22%7D" host=xxxxxx.herokuapp.com request_id=a7eddbef-d40f-4d2e-93a7-edf160ff6dcc fwd="173.230.155.65" dyno=web.1 connect=4ms service=8ms status=403 bytes=273My keys from index.js thats running on my parser server
var api = new ParseServer({ databaseURI: databaseUri || 'mongodb://xxx:[email protected]:43694/awslike', cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js', appId: process.env.APP_ID || 'X5hlsQoD2uY0Za2zQUyy', javascriptKey: process.env.CLIENT_KEY || '6HkIJlJxgVD9N7Qsn9cw', masterKey: process.env.MASTER_KEY || 'GhgoqbRnKkHN9ctsPAYu', clientKey: process.env.CLIENT_KEY || '6HkIJlJxgVD9N7Qsn9cw', restAPIKEY: process.env.RESTAPI_KEY || 'X3eX9XwzzJpkmMfjLR8E', fileKey: process.env.FILE_KEY || 'PQAvUKDvBf0rg7GMhnZT', filesAdapter: new S3Adapter( process.env.AWS_ACCESS_KEY || 'xxxxxxx', process.env.AWS_SECRET_ACCESS_KEY || 'xxxxxxx', {bucket: process.env.AWS_BUCKET_NAME || 'xxxxx', bucketPrefix: "", directAccess: true} ) });I dont care about the keys being posted here, these are just temp but I wanted to display that they match the php ones..
Printing out the url from the ParseClient gives me this..
https://xxxxx.herokuapp.com/parse/classes/UserTag?where=%7B%22gameID%22%3A%22241e84cf-055e-43b8-a5ad-75c7c87c75ea%22%7Dreturned value: unauthorized.
Either im missing something simple or something is broke. @gfosco any thoughts on how I could help debug this further.. ?
EDIT: I should mention JS and CURL requests work.
According to that code, you're passing
restAPIKEYinstead ofrestAPIKey... My advice is to remove all keys except master from the ParseServer. No morejavascriptKey,clientKey,restAPIKey, ordotNetKey.@gfosco ok this is a bit confusing since ill need the js key for my js code, php for rest api, and master.. also our app will be using the client key.. So ideally I need these keys unless Im missing something.
making the change for index.js to
restAPIKeY: process.env.RESTAPI_KEY || 'X3eX9XwzzJ34MfjLR8E',Gave me the same results.
Disabling clientKey and javascriptKey and clientKey in index.js solved the problem.
However this begs the question then on the JS side of things you have to embed your master key which could bee seen in source code if your doing front end stuff, isnt this a security issue? See concern at #352
` <script type="text/javascript">
Parse.initialize("X5h34242uY034Uyy", "6H342gVD234Qsn9cw"); Parse.serverURL = 'https://xxxxx.herokuapp.com/parse' `btw thank you for taking the time with this.
You don't embed the master key anywhere. It's just that with parse-server, you don't need client keys, the app id is sufficient identification. They don't add any extra security on parse-server, since they are provided together in visible client code.
You say not to set any of the client keys, I assume your saying in the app.js file for parse-server.
But in the the Parse Server Guide the SDK Usage says to initialize PHP with the following.
ParseClient::initialize('YOUR_APP_ID', 'YOUR_CLIENT_KEY', 'YOUR_MASTER_KEY'); ParseClient::setServerURL('http://localhost:1337/parse');All of the other SDKs just say to use the APP_ID.
Im having a heck of a time getting all these Keys and SDK's and CloudCode to work right.
Reading a lot of the issue posts it doesn't seem as thou Im the only one either.I applaud the community for everyone supporting each other thou.
@petek157 did you ever find a solution to this? I'm still stuck on the PHP SDK giving me an unauthorized error.
Well.... I did get it working, but its been so long that I really dont remember what was going on then or what I did to fix it.
If you dont have a CLIENT KEY in your app file, try putting a random string in your ParseClient::initialize call for the YOUR_CLIENT_KEY. I know once apon a time that was something that I did that fix a problem. Ha whether it was this one or not I really cant remember.
Thanks for the reply @petek157. I'm using AWS' Elastic Beanstalk implementation and it doesn't include a client/api key. So I have a random string in my ParseClient::initialize call now, but it's still giving that unauthorized error. Struggling to find any resolution outside of potentially this being the issue... parse-community/parse-php-sdk#225
Not really sure how to resolve this.
This works for me.. here is a snippet of code I'm using in our framework.
use Parse\ParseQuery; use Parse\ParseACL; use Parse\ParsePush; use Parse\ParseUser; use Parse\ParseInstallation; use Parse\ParseException; use Parse\ParseFile; use Parse\ParseClient; use Parse\ParseGeoPoint; $master_key = variable_get('parse_api_master', NULL); $app_id = variable_get('parse_api_appid', NULL); $rest_key = variable_get('parse_api_restkey', NULL); $server = variable_get('parse_api_url', NULL); $path = variable_get('parse_api_mount', NULL); ParseClient::initialize($app_id, $rest_key, $master_key); ParseClient::setServerURL($server, $path); // example query try { $query = new ParseQuery($class); // deal with date ranges.. must be mm/dd/yyyy, converted to parse date object $query->greaterThanOrEqualTo($dateType, $startDate); $query->lessThanOrEqualTo($dateType, $endDate); $results = $query->find($useMasterKey = true); // <-- use master key!! $data['count'] = count($results); return $data; } catch (\Exception $e) { echo $e->getMessage(); }make sure when doing queries you have $useMasterKey = true set in a ->find or what ever the end query is..
@gateway that was my issue! Thank you so much for the help there. Do you know if there's a way to do a ->get('ID') or do you have to use ->find() and limit it to 1 result?
any sort of ->first($useMasterKey = true) etc, use the master key inside..
so if you only want one record use first, otherwise find etc..
Hello!
I have a Parse server running and working well with my Swift code. Now, I would like to connect this MongoDB server to my PHP code. But I can't find any documentation on how to do that.
For the moment, I have this code :
ParseClient::initialize( $app_id, $rest_key, $master_key );How to update it to add my server URL ?
Thanks!
Axel