public function doValidation(Response $response) { $body = (string) $response->getBody(); $json = json_decode($body); if (!$json) { throw new ValidationFailedException('The given json document is empty or not valid json.'); } $store = new JsonStore($json); $error = false; $noCorrectJsonPaths = array(); foreach ($this->jsonPaths as $path) { $jsonValue = $store->get($path['pattern']); $count = count($jsonValue); if ($jsonValue === FALSE || is_array($jsonValue) && empty($jsonValue)) { $error = true; $noCorrectJsonPaths[] = $path['pattern'] . ' (JSON Path not found)'; } if ($this->checkRelation($path['relation'], $path['value'], $count) === false) { $error = true; $noCorrectJsonPaths[] = $path['pattern'] . ' (number of JSONPaths is not correct corresponding to the given relation/value)'; } } if ($error === true) { $allNoCorrectJsonPaths = implode('", "', $noCorrectJsonPaths); throw new ValidationFailedException('Disonances with JSON Paths "' . $allNoCorrectJsonPaths . '!'); } }
/** * @param $latitude * @param $longitude * @return \Manticora\WeatherCenter\Domain\Model\Weather\Weather */ public function findByLocationAndDateTime($latitude, $longitude, DateTimeInterface $dateTime) { $response = $this->client->get('http://api.wunderground.com/api/' . $this->key . '/lang:' . $this->language . '/hourly10day/q/' . $latitude . ',' . $longitude . '.json'); $o = json_decode($response->getContent(), true); $store = new JsonStore($o); $pattern = "\$..hourly_forecast[?(@.FCTTIME.hour == {$dateTime->getHour()}\n && @.FCTTIME.mday == {$dateTime->getDay()}\n && @.FCTTIME.mon == {$dateTime->getMonth()} )]"; $res = $store->get(str_replace(array("\n", "\r"), '', $pattern)); if (!isset($res[0])) { return null; } $weat = $res[0]; $weather = new Weather($weat['condition'], $weat['icon_url'], $weat['temp']['metric'], $weat['humidity'], $weat['pop'], $weat['wspd']['metric'], $weat['wdir']['degrees']); return $weather; }
public function testGetAllByKeyFiltered() { $data = $this->jsonStore->get("\$..book[(@.code=='02.01')].category"); $expected = ["fiction", "fiction"]; $this->assertEquals($data, $expected); }
/** * 生成请求的示例和说明 * * @param array $api * @return array [sample, doc] */ private function createRequestDoc($api) { //TODO: 需要处理特殊情况: 输入被绑定在多个参数, 或者输入的不同重叠区域被绑定到不同参数时 $docs = ''; // 提取参数 $params = new JsonStore(array()); foreach ($api['params'] as $name => $param) { $ori = $params->get($param['value']); if (count($ori) !== 0) { // 现在不支持同一个变量多个地方引用 continue; } $info = new \ArrayObject(array($name, $param)); $params->set($param['value'], $info); } $params = $params->toArray(); // 路由中指定的路径 $route_path = HttpRouterEntries::stringToPath($api['uri'][1]); // 这是绝对路径 $path = $api['uri'][0]; // 路径拼到示例中 if (isset($params['path'])) { $req_path = $params['path']; // 请求中使用的路径, 这是相对路径 $offest = count(HttpRouterEntries::stringToPath($api['root'])); // 相对于绝对路径的偏移 if (is_array($req_path)) { // 参数只是路径的一部分 if (count($req_path) > 0) { $end = max(array_keys($req_path)); Verify::isTrue($end < 128, "too long path with length {$end}"); for ($i = 0; $i <= $end; $i++) { if (isset($req_path[$i])) { list($arg_name, $arg_info) = $req_path[$i]; if (isset($route_path[$i + $offest]) && $route_path[$i + $offest] !== '*') { //忽略固定的路径 } else { $route_path[$i + $offest] = "[{$arg_name}]"; $docs = "{$docs}{$arg_name}:\r\n {$arg_info['doc']}\r\n\r\n"; } } else { if (!isset($route_path[$i + $offest])) { $route_path[$i + $offest] = '*'; } } } } } else { // 参数整个路径 list($arg_name, $arg_info) = $req_path; $route_path[$offest] = "[{$arg_name}]"; $docs = "{$docs}{$arg_name}:\r\n {$arg_info['doc']}\r\n\r\n"; } unset($params['path']); } $path .= ' /'; $path .= implode('/', $route_path); // querystring if (isset($params['_GET'])) { $get = $params['_GET']; if (is_array($get)) { $first = true; foreach ($get as $name => $value) { list($arg_name, $arg_info) = $value; if ($first) { $path = $path . '?'; $first = false; } else { $path = $path . '&'; } $path = "{$path}{$name}=[{$arg_name}]"; $docs = "{$docs}{$arg_name}:\r\n {$arg_info['doc']}\r\n\r\n"; } } else { // 参数整个_GET list($arg_name, $arg_info) = $get; $path = "{$path}?[{$arg_name}]"; $docs = "{$docs}{$arg_name}:\r\n {$arg_info['doc']}\r\n\r\n"; } unset($params['_GET']); } $path .= " HTTP/1.1\r\n"; // header $header = ''; if (isset($params['header'])) { $headers = $params['header']; $first = true; foreach ($headers as $header_name => $value) { //if (substr_compare($name, 'HTTP_X_', 0, 7) !== 0) { // continue; //} //$words = explode('_', substr($name, 7)); //$header_name = ''; //foreach ($words as $k => $word) { // $words[$k] = ucwords(strtolower($word)); //} //$header_name = implode('-', $words); list($arg_name, $arg_info) = $value; $header = "{$header}{$header_name}: [{$arg_name}]\r\n"; $docs = "{$docs}{$arg_name}:\r\n {$arg_info['doc']}\r\n\r\n"; unset($params['_SERVER'][$name]); } } // cookie $header = ''; if (isset($params['_COOKIE'])) { $cookies = $params['_COOKIE']; $first = true; $header = $header . "Cookie: "; foreach ($cookies as $cookie_name => $value) { list($arg_name, $arg_info) = $value; $header = "{$header}{$cookie_name}=[{$arg_name}];"; $docs = "{$docs}{$arg_name}:\r\n {$arg_info['doc']}\r\n\r\n"; } $header .= "\r\n"; } // body $body = ''; if (isset($params['_POST'])) { $post = $params['_POST']; $first = true; if (is_array($post)) { foreach ($post as $name => $value) { list($arg_name, $arg_info) = $value; if ($first) { $first = false; } else { $body = $body . '&'; } $body = "{$body}{$name}=[{$arg_name}]"; $docs = "{$docs}{$arg_name}:\r\n {$arg_info['doc']}\r\n\r\n"; } } else { // 参数整个_POST list($arg_name, $arg_info) = $post; $body = "{$body}[{$arg_name}]"; $docs = "{$docs}{$arg_name}:\r\n {$arg_info['doc']}\r\n\r\n"; } unset($params['_POST']); } if (isset($params['_FILES'])) { $files = $params['_FILES']; if (is_array($files)) { foreach ($files as $name => $value) { //TODO: 这里假设只有一个文件上传 list($arg_name, $arg_info) = $this->searchArgInfo($value); $docs = "{$docs}{$name}:\r\n {$arg_info['doc']}\r\n\r\n"; } } unset($params['_POST']); } $sample = $path . $header . "\r\n" . $body; return array($sample, $docs); }
/** * @brief Retrieves full client data node (not only text) * @param $jpath [in] node name to be extracted * @param $context [in] context used for looking for node with specified name. * @return all matching nodes as array (each node format like json_decode in array mode) * @throws AfsNoResultException */ public function get_nodes($jpath = null, $unused = array()) { if (is_null($jpath) || $jpath === "") { // return json as array return array(json_decode(json_encode($this->client_data->contents), true)); } $store = new JsonStore($this->client_data->contents); $result = $store->get($jpath, false); if (!empty($result)) { return $result; } else { throw new AfsNoResultException(); } }
private function getDataPath($data) { $selectedNode = $data; if ($this->validateDataPath()) { $store = new JsonStore($data); $path = $this->options['path']; // Returns an array with all categories from books which have an isbn attribute $selectedNode = $store->get($path); } return $selectedNode; }
protected function latexmlfunctions($type = null, $text = null) { global $CFG, $DB; if (empty($type) || empty($text) || !in_array($type, array('latex2image', 'latex2mathml', 'mathml2image', 'mathml2latex'))) { return "@500"; } $text_md5 = md5($text); $download_count = $DB->count_records_sql('SELECT count(*) FROM {quiz_nitroreportpdf_latex_db} WHERE type="' . $type . '" AND ratio>0'); if ($download_count > 0) { $download = $DB->get_records_sql('SELECT * FROM {quiz_nitroreportpdf_latex_db} WHERE type="' . $type . '" AND ratio>0 ORDER BY ratio ASC'); foreach ($download as $download) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://' . parse_url($download->url)['host']); curl_setopt($ch, CURLOPT_AUTOREFERER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_MAXREDIRS, 10); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_exec($ch); if (!curl_errno($ch)) { $info = curl_getinfo($ch); if ($info['http_code'] != 200) { curl_close($ch); return "@500"; break; } else { curl_close($ch); switch (strtoupper($download->typesender)) { case 'HTTP-GET': $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $download->url . '?' . preg_replace('/#magic#/', $text, $download->options_url)); curl_setopt($ch, CURLOPT_AUTOREFERER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_MAXREDIRS, 10); curl_setopt($ch, CURLOPT_TIMEOUT, 120); $req = curl_exec($ch); if (curl_errno($ch)) { return "@500"; break; } else { if (in_array($download->format, array('GIF', 'JPG', 'PNG', 'SVG')) && (empty($download->path) || $download->path == NULL)) { file_put_contents($CFG->dirroot . '/mod/quiz/report/nitroreportpdf/cache/' . $text_md5 . '.' . strtolower($download->format), $req); return $CFG->dirroot . '/mod/quiz/report/nitroreportpdf/cache/' . $text_md5 . '.' . strtolower($download->format); } elseif (preg_match('/JSON/', $download->format)) { $format = explode('-', $download->format); $format = $format[1]; $store = new JsonStore($req); $res = $store->get('$' . $download->path, true); file_put_contents($CFG->dirroot . '/mod/quiz/report/nitroreportpdf/cache/' . $text_md5 . '.' . $format, $res[0]); if ($format == "TEXT") { return $req; } else { return $CFG->dirroot . '/mod/quiz/report/nitroreportpdf/cache/' . $text_md5 . '.' . $format; } } elseif ($download->format == "TEXT") { return $req; } } break; case 'HTTP-POST': $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $download->url); curl_setopt($ch, CURLOPT_AUTOREFERER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_MAXREDIRS, 10); curl_setopt($ch, CURLOPT_TIMEOUT, 120); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, preg_replace('/#magic#/', $text, $download->options_url)); $req = curl_exec($ch); if (curl_errno($ch)) { curl_close($ch); return "@500"; break; } else { curl_close($ch); if (in_array($download->format, array('GIF', 'JPG', 'PNG', 'SVG')) && (empty($download->path) || $download->path == NULL)) { file_put_contents($CFG->dirroot . '/mod/quiz/report/nitroreportpdf/cache/' . $text_md5 . '.' . strtolower($download->format), $req); return $CFG->dirroot . '/mod/quiz/report/nitroreportpdf/cache/' . $text_md5 . '.' . strtolower($download->format); } elseif (preg_match('/JSON/', $download->format)) { $format = explode('-', $download->format); $format = $format[1]; $store = new JsonStore($req); $res = $store->get('$' . $download->path, true); file_put_contents($CFG->dirroot . '/mod/quiz/report/nitroreportpdf/cache/' . $text_md5 . '.' . $format, $res[0]); if ($format == "TEXT") { return $req; } else { return $CFG->dirroot . '/mod/quiz/report/nitroreportpdf/cache/' . $text_md5 . '.' . $format; } } elseif ($download->format == "TEXT") { return $req; } } break; } break; } } touch($CFG->dirroot . '/mod/quiz/report/nitroreportpdf/cache/' . $text_md5 . '.' . strtolower($download->format)); } } else { return "@500"; } }