Skip to content

Instantly share code, notes, and snippets.

@modeverv
Created December 3, 2018 08:41
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 modeverv/8557b0eb6553864bb16de49b1ffe8b0a to your computer and use it in GitHub Desktop.
Save modeverv/8557b0eb6553864bb16de49b1ffe8b0a to your computer and use it in GitHub Desktop.
<?php
/*
curl -X GET -G \
-H "X-NCMB-Application-Key: eda167f64692337c2bd01e25f9ebbc3d03967ca5fd2b398c7ee8c0b393127436" \
-H "X-NCMB-Timestamp: 2018-11-30T07:41:48.769Z" \
-H "X-NCMB-Signature: jQg0MIuTewb5V8NiiXy3EkcmBYdm5Uxa6Hwg2Nk4JzA=" \
-H "Content-Type: application/json" \
--data-urlencode 'createDate={"$gte":"2018-10-30","$lte":"2018-10-30"}' \
https://mbaas.api.nifcloud.com/2013-09-01/classes/first
*/
$base_url = 'https://mbaas.api.nifcloud.com/2013-09-01/classes';
// https://mbaas.api.nifcloud.com/2013-09-01/classes/first
$query = "where=" . urlencode(json_encode(
["createDate" =>
['$gte' => "2018-10-30", '$lte' => "2018-12-30"]
]
));//これがやりたい
$query = "where=" . urlencode(json_encode(
["name" => "testuser"]
));//出た
//なんだこれ
// {"date":{"$gte":{"__type":"Date","iso":"1994-02-18T03:00:00.000Z"}}}'
// {"createDate":{"$gte":{"__type":"Date","iso":"2018-10-01T00:00:00.000Z"}}}'
$query = "where=" . urlencode(json_encode(
["createDate" => ['$gte' => ["__type" => "Date","iso" => "2018-10-01T00:00:00.000Z"]]]
));//でた
// というわけで
$query = "where=" . urlencode(json_encode(
[
"createDate" => [
'$gte' => ["__type" => "Date","iso" => "2018-10-01T00:00:00.000Z"],
'$lte' => ["__type" => "Date","iso" => "2018-12-01T00:00:00.000Z"],
]
]
));//2件
$query = "where=" . urlencode(json_encode(
[
"createDate" => [
'$gte' => ["__type" => "Date","iso" => "2018-10-01T00:00:00.000Z"],
'$lte' => ["__type" => "Date","iso" => "2018-12-31T00:00:00.000Z"],
]
]
));//3件
$curl = curl_init();
//curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_URL, $base_url . '/first' . '?' . $query );
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$method = 'GET';
$fqdn = 'mbaas.api.nifcloud.com';
$api_version = '2013-09-01';
$path = 'classes/first';
$timestamp = "2018-11-30T07:41:48.769Z";
$application_key = 'eda167f64692337c2bd01e25f9ebbc3d03967ca5fd2b398c7ee8c0b393127436';
$client_key = 'xxxx';
$header_string = "SignatureMethod=HmacSHA256&";
$header_string .= "SignatureVersion=2&";
$header_string .= "X-NCMB-Application-Key=" . $application_key . "&";
$header_string .= "X-NCMB-Timestamp=". $timestamp;
$header_string .= $query == "" ? "" : "&" . $query;
$signature_string = $method . "\n";
$signature_string .= $fqdn . "\n";
$signature_string .= "/" . $api_version . "/" . $path ."\n";
$signature_string .= $header_string;
$signature = base64_encode(hash_hmac("sha256", $signature_string, $client_key, true));
//var_dump($signature_string);
//var_dump($signature);
//$signature_base = "GET\n";
//$signature_base .= "mbaas.api.nifcloud.com\n";
//$signature_base .= "/2013-09-01/classes/TestClass\n";
//$signature_base .= "SignatureMethod=HmacSHA256&SignatureVersion=2&X-NCMB-Application-Key=6145f91061916580c742f806bab67649d10f45920246ff459404c46f00ff3e56&X-NCMB-Timestamp=2013-12-02T02:44:35.452Z&where=%7B%22testKey%22%3A%22testValue%22%7D";
//var_dump($signature_base);
//$signature = base64_encode(hash_hmac('sha256', $signature_base, '1343d198b510a0315db1c03f3aa0e32418b7a743f8e4b47cbff670601345cf75', true));
//exit();
$headers = [
'X-NCMB-Application-Key: eda167f64692337c2bd01e25f9ebbc3d03967ca5fd2b398c7ee8c0b393127436',
'X-NCMB-Signature: ' . $signature,
'X-NCMB-Timestamp: 2018-11-30T07:41:48.769Z',
'Content-Type: application/json',
];
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
$result = json_decode($response, true);
curl_close($curl);
echo "<pre>\n";
echo "storageを検索するサンプル\n\n";
echo "条件:\n";
var_dump(urldecode($query));
echo "結果:\n";
var_dump($result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment