简体   繁体   English

从parse.com获取REST响应以获取搜索功能-PHP

[英]Get REST response from parse.com for search functionality - PHP

I'm creating a search functionality for my website and I'm using Parse.com for storing data. 我正在为我的网站创建搜索功能,并且正在使用Parse.com来存储数据。 I have a class Posts with a "plainContent" column which stores the content of my article in plain text format. 我有一个带有“ plainContent”列的Posts类,该列以纯文本格式存储文章的内容。 I've found this article: 我发现这篇文章:

http://blog.parse.com/learn/engineering/implementing-scalable-search-on-a-nosql-backend/ 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行,在我的情况下将是:

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

I can't create the curl query with PHP. 我无法使用PHP创建curl查询。 What exactly is the $all variable in the example? 示例中的$ all变量到底是什么? Here's my php script: 这是我的PHP脚本:

$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? $ all变量应该是什么?

$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);

Assuming $all is needed as it is you have shown in your code (as per parse.com). 假设需要$all ,因为它已在代码中显示(根据parse.com)。 Here are few catch. 这里有一些收获。

1) You are doing urlencode over the whole request parameter. 1)您正在整个请求参数上执行urlencode But actually you have to do it over the value of where in your example. 但实际上你必须做过来的值where在您的示例。 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. 2)您在此处发布的数据不是json,实际上是key=value解析的数据。 So remove Content-Type: application/json thing from your code. 因此,从您的代码中删除Content-Type: application/json东西。

That's all! 就这样!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM