Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
fraxool opened this issue Feb 1, 2016 · 27 comments
Closed

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

fraxool opened this issue Feb 1, 2016 · 27 comments

Comments

@fraxool
Copy link

fraxool commented Feb 1, 2016

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

@MarkCYoung
Copy link

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

@fraxool
Copy link
Author

fraxool commented Feb 1, 2016

@MarkCYoung Thanks for your answer.

That's what I did until now. I also set up the different keys on my parse-server but also in my PHP configuration file... And I always get the message "unauthorized" on all of my requests. Still trying to fix that, but I'm opened to your suggestions!

EDIT : 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 👍

@danielb93
Copy link

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.

@gfosco
Copy link
Contributor

gfosco commented Feb 2, 2016

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

@a9austin
Copy link

a9austin commented Feb 4, 2016

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

@mauriciord
Copy link

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

@fraxool
Copy link
Author

fraxool commented Feb 8, 2016

Sorry for the late guys. I wrote a complete answer on StackOverflow to fix that :

http://stackoverflow.com/a/35132450/4569921

@gateway
Copy link

gateway commented Feb 9, 2016

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

@gateway
Copy link

gateway commented Feb 9, 2016

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?

@gateway
Copy link

gateway commented Feb 10, 2016

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

@occurrentarts
Copy link

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

@gateway
Copy link

gateway commented Feb 12, 2016

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

@occurrentarts
Copy link

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

montymxb pushed a commit to montymxb/parse-server that referenced this issue Feb 14, 2016
Updated push handling for local or non-local time push.
@gfosco
Copy link
Contributor

gfosco commented Feb 16, 2016

I updated the PHP SDK to version 1.2.0, you can now change the server URL after initialization:

ParseClient::setServerURL('https://myserver.com/parse');

@gfosco gfosco closed this as completed Feb 16, 2016
@occurrentarts
Copy link

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

@gfosco
Copy link
Contributor

gfosco commented Feb 16, 2016

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

@gateway
Copy link

gateway commented Feb 16, 2016

@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:rxxx@xxxx.mongolab.com: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.

@gfosco
Copy link
Contributor

gfosco commented Feb 16, 2016

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.

@gfosco gfosco removed the in-process label Feb 16, 2016
@gateway
Copy link

gateway commented Feb 16, 2016

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

@gfosco
Copy link
Contributor

gfosco commented Feb 16, 2016

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.

@petek157
Copy link

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.

@jakelisby
Copy link

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

@petek157
Copy link

petek157 commented Dec 1, 2016

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.

@jakelisby
Copy link

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.

@gateway
Copy link

gateway commented Dec 1, 2016

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

@jakelisby
Copy link

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

@gateway
Copy link

gateway commented Dec 1, 2016

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants