1

I'm creating a search functionality for my website and I'm using Parse.com for storing data. I have a class Posts with a "plainContent" column which stores the content of my article in plain text format. I've found this article:

http://blog.parse.com/learn/engineering/implementing-scalable-search-on-a-nosql-backend/

which is very useful. I've added the cloud code which splits my plain text into single words and puts them into an array. Now I have my Posts with an extra column "words" which stores an array with all the single words for article's content. I got the the step of retrieving data now but the problem is the following code:

curl -v -X GET  
    -H "X-Parse-Application-Id: ${APPLICATION_ID}" 
    -H "X-Parse-REST-API-Key: ${REST_API_KEY}" 
    -G 
    --data-urlencode 'where={"hashtags":{"$all":["#parse", "#ftw"]}}' 
    "https://api.parse.com/1/classes/Post"

precisely the --data-urlencode row which in my case would be:

--data-urlencode 'where={"words":{"$all":["word1", "word2"]}}' 

I can't create the curl query with PHP. What exactly is the $all variable in the example? Here's my php script:

$url = 'https://api.parse.com/1/classes/Posts?';

$headers = array(
    "Content-Type: application/json",
    "X-Parse-Application-Id: " . $MyApplicationId,
    "X-Parse-REST-API-Key: " . $MyParseRestAPIKey
);

$query = urlencode('where={"words":{"$all":["parseobjectcontains", "compatible"]}}');
$ch = curl_init($url.$query);

$handle = curl_init(); 
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($handle);
curl_close($handle);

$array = json_decode($data);

var_dump($array);

This script returns all results without filtering.

The question is how to build this sentence to reflect the example in the article from the link? What should be that $all variable?

$query = urlencode('where={"words":{"$all":["parseobjectcontains", "compatible"]}}');

EDIT

I had an error in my script:

$ch = curl_init($url.$query);
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $url);

Correction:

$handle = curl_init($url.$query);
    //$handle = curl_init();
    //curl_setopt($handle, CURLOPT_URL, $url);
2
  • $all is not a php variable, its just a string with a dollar sign in it, used as part of the query syntax for mongodb Commented Nov 26, 2015 at 10:29
  • @steve not it's all clear about it. thanks Commented Nov 26, 2015 at 11:17

1 Answer 1

2

Assuming $all is needed as it is you have shown in your code (as per parse.com). Here are few catch.

1) You are doing urlencode over the whole request parameter. But actually you have to do it over the value of where in your example. So change it as below:

$query = 'where='.urlencode('{"words":{"$all":["parseobjectcontains", "compatible"]}}');

2) The data you are posting here is not json, it is actually key=value pared data. So remove Content-Type: application/json thing from your code.

That's all!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.