bdizzle
bdizzle

Reputation: 419

PHP curl parse.com parameters

Parse limits it's results to 100. I'd like to set the limit higher so that I can loop through it. Their cURL example is done like

curl -X GET \
  -H "X-Parse-Application-Id: ${APPLICATION_ID}" \
  -H "X-Parse-REST-API-Key: ${REST_API_KEY}" \
  -G \
  --data-urlencode 'limit=200' \
  --data-urlencode 'skip=400' \
  https://api.parse.com/1/classes/GameScore

I've written my code for cURL in PHP, but unsure how to incorporate the limit and skip. I reviewed documentation here, but unsure of what it matches to. Here is my code

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

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

Upvotes: 1

Views: 386

Answers (1)

fvu
fvu

Reputation: 32953

What they're doing there with command line cURL will make cURL build an URL like

https://api.parse.com/1/classes/GameScore?limit=200&skip=400

As explained in the cURL documentation, that's what the -G parameter does, it converts --data arguments (normally destined to define POST fields) into get parameters.

The safest/easiest way would be to compose the query string using http_build_query and tack the result of that call to the end of the url you give to CURLOPT_URL. Like this:

$parameters = array('limit' => 200, 'skip' => 400); 
$url = $url . '?' . http_build_query($parameters);
...
curl_setopt($handle, CURLOPT_URL, $url);

Of course, if you're certain that your parameters will always be simple integers (not requiring URL encoding), you could also use simple string functions, like this:

$url = sprintf("%s?limit=%d&skip=%d",$url,$limit,$skip);
...
curl_setopt($handle, CURLOPT_URL, $url);

Upvotes: 3

Related Questions