Skip to content

Instantly share code, notes, and snippets.

@dmitriz
Created December 13, 2013 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitriz/7942135 to your computer and use it in GitHub Desktop.
Save dmitriz/7942135 to your computer and use it in GitHub Desktop.
Parse PHP Client
<?php
class ParseStore implements OutStore {
/** Creating new hashes on Parse DB
* @param string $schema - Parse schema (class)
* @param array $array - Array of Hashes
* @return array $ids - Parse IDs of entries created
*/
public function createMany ($schema, array $bundle) {
$bundle = $this->_moveIds($bundle);
$data = array();
foreach ($bundle as $entry) {
$dataEntry = array(
'method' => 'POST',
'path' => '/1/classes/' . $schema,
'body' => $entry,
);
$data[] = $dataEntry;
}
$response = $this->_request( 'POST', PARSE_URL . '/batch', array('requests' => $data) );
$ids = array();
foreach ($response as $entry) {
if ( isset($entry['success']) ) {
array_push($ids, $entry['success']['objectId']);
} else {
array_push($ids, '');
}
}
return $ids;
}
// It seems that Parse ignores 'id' keys and replaces them with actual Object Ids
// so I am moving 'id' to 'i'
private function _moveIds (array $bundle) {
$result = array();
foreach ($bundle as $hash) {
$hash['i'] = $hash['id'];
unset($hash['id']);
array_push($result, $hash);
}
return $result;
}
private function _request ($httpMethod, $url, array $data) {
$c = curl_init($url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_FAILONERROR, true);
curl_setopt($c, CURLOPT_CUSTOMREQUEST, $httpMethod);
curl_setopt($c, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($c, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'X-Parse-Application-Id: ' . PARSE_APP_ID,
'X-Parse-REST-API-Key: ' . PARSE_REST_KEY,
));
if (PROXY) {
curl_setopt($c, CURLOPT_PROXY, PROXY);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
}
$response = curl_exec($c);
if (curl_error($c)) {
throw new exception("Backend Connection Error: |" . curl_error($c) . "|, Response: |$response|");
}
curl_close($c);
return json_decode($response, true);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment