Skip to content

How to use parse-server with the PHP SDK ? #124

Description

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

Activity

  1. MarkCYoung commented on Feb 1, 2016

    @MarkCYoung

    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';

  2. danielb93 commented on Feb 1, 2016

    @danielb93

    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.

  3. gfosco commented on Feb 2, 2016

    @gfosco
    Contributor

    I need to update the PHP SDK with a method for changing the server URL... I missed it.

  4. a9austin commented on Feb 4, 2016

    @a9austin

    @fraxool would you be willing to share your source code on how you got your swift code connected to your parse server?

  5. mauriciord commented on Feb 4, 2016

    @mauriciord

    Waiting for method to change server URL ... thanks !
    Posted here this issue at PHP SDK.

  6. gateway commented on Feb 9, 2016

    @gateway

    @gfosco any updates to the php sdk you can push please?

  7. gateway commented on Feb 9, 2016

    @gateway

    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=273

    any ideas?

  8. gateway commented on Feb 10, 2016

    @gateway

    Anyone have any ideas.. im pretty stuck from moving forward from my comment above ^^^

  9. occurrentarts commented on Feb 12, 2016

    @occurrentarts

    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...

  10. gateway commented on Feb 12, 2016

    @gateway

    @occurrentarts what did you change to make this work.. I'm currently stuck on the php side of things :(

  11. occurrentarts commented on Feb 12, 2016

    @occurrentarts

    @gateway

    1. first i updated my parse php sdk to the newest
    2. then i opened ParseClient.php and set the hostname equal to my server:
      const HOST_NAME = 'http://parseserver-bv###-env.elasticbeanstalk.com';
    3. then i changed the API_Version key like so:
      const API_VERSION = 'parse';
    4. then i opened ParseFile.php and added my file key to the path ( line 227? )
      $url = ParseClient::getAPIUrl().'files/my_file_key/'.$this->getName();
    5. 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:

    http://stackoverflow.com/questions/18908426/increasing-client-max-body-size-in-nginx-conf-on-aws-elastic-beanstalk

    finally i have object saving and image and audio uploads working with the PHP SDK.

  12. added a commit that references this issue on Feb 14, 2016
  13. 1 remaining item

  14. occurrentarts commented on Feb 16, 2016

    @occurrentarts

    thanks @gfosco
    does it continue to require the fileKey or is that taken care of by parse-server?

  15. gfosco commented on Feb 16, 2016

    @gfosco
    Contributor

    @occurrentarts fileKey should only be required on the parse-server side, for legacy files hosted by Parse.

  16. gateway commented on Feb 16, 2016

    @gateway

    @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=273

    My 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%7D

    returned 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.

  17. gfosco commented on Feb 16, 2016

    @gfosco
    Contributor

    According to that code, you're passing restAPIKEY instead of restAPIKey... My advice is to remove all keys except master from the ParseServer. No more javascriptKey, clientKey, restAPIKey, or dotNetKey.

  18. gateway commented on Feb 16, 2016

    @gateway

    @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.

  19. gfosco commented on Feb 16, 2016

    @gfosco
    Contributor

    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.

  20. petek157 commented on Feb 24, 2016

    @petek157

    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.

  21. jakelisby commented on Dec 1, 2016

    @jakelisby

    @petek157 did you ever find a solution to this? I'm still stuck on the PHP SDK giving me an unauthorized error.

  22. petek157 commented on Dec 1, 2016

    @petek157

    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.

  23. jakelisby commented on Dec 1, 2016

    @jakelisby

    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.

  24. gateway commented on Dec 1, 2016

    @gateway

    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..

  25. jakelisby commented on Dec 1, 2016

    @jakelisby

    @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?

  26. gateway commented on Dec 1, 2016

    @gateway

    any sort of ->first($useMasterKey = true) etc, use the master key inside..

    so if you only want one record use first, otherwise find etc..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions