/** * @inherit */ public function makeRequest($method, $path, $data = array()) { $url = $this->getEndpoint() . $path; $options = array(CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, CURLOPT_HTTPHEADER => array('Accept: application/json', 'Content-type: application/json', 'User-Agent: ' . Version::userAgent()), CURLOPT_CUSTOMREQUEST => self::GET, CURLOPT_POST => false, CURLOPT_POSTFIELDS => null); if ($this->username && $this->password) { $options[CURLOPT_HTTPAUTH] = CURLAUTH_BASIC; $options[CURLOPT_USERPWD] = $this->username . ':' . $this->password; } switch ($method) { case self::DELETE: $options[CURLOPT_CUSTOMREQUEST] = self::DELETE; break; case self::POST: case self::PUT: $dataString = $this->encodeData($data); $options[CURLOPT_CUSTOMREQUEST] = $method; $options[CURLOPT_POSTFIELDS] = $dataString; $options[CURLOPT_HTTPHEADER][] = 'Content-Length: ' . strlen($dataString); if (self::POST == $method) { $options[CURLOPT_POST] = true; } break; } //additional curl options if (\Everyman\Neo4j\DI::isRegistered("curlOptions")) { $additionalOptions = \Everyman\Neo4j\DI::resolve("curlOptions"); if (is_array($additionalOptions)) { $options = array_replace($options, $additionalOptions); } unset($additionalOptions); } $ch = $this->getHandle(); curl_setopt_array($ch, $options); $response = curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); if (!$code) { $code = 500; $headerSize = 0; $response = json_encode(array("error" => curl_error($ch) . ' [' . curl_errno($ch) . ']')); } $bodyString = substr($response, $headerSize); $bodyData = json_decode($bodyString, true); $headerString = substr($response, 0, $headerSize); $headers = explode("\r\n", $headerString); foreach ($headers as $i => $header) { unset($headers[$i]); $parts = explode(':', $header); if (isset($parts[1])) { $name = trim(array_shift($parts)); $value = join(':', $parts); $headers[$name] = $value; } } return array('code' => $code, 'headers' => $headers, 'data' => $bodyData); }
/** * Return the path to use * * @return string */ protected function getPath() { $info = $this->client->getServerInfo(); if (isset($info['cypher'])) { $url = $info['cypher']; } else { if (isset($info['extensions']['CypherPlugin']['execute_query'])) { $url = $info['extensions']['CypherPlugin']['execute_query']; } else { throw new Exception('Cypher unavailable'); } } return preg_replace('/^.+' . preg_quote(DI::resolve("dbPath"), "/") . '/', '', $url); }
/** * @inherit */ public function makeRequest($method, $path, $data = array()) { $url = $this->getEndpoint() . $path; $context_options = array('http' => array('method' => 'GET', 'ignore_errors' => true, 'header' => "Content-type: application/json\r\n" . "Accept: application/json\r\n" . "User-Agent: " . Version::userAgent() . "\r\n")); if ($this->username && $this->password) { $context_options[$this->scheme]['header'] .= 'Authorization: Basic ' . base64_encode($this->username . ':' . $this->password) . "\r\n"; } switch ($method) { case self::DELETE: $context_options[$this->scheme]['method'] = self::DELETE; break; case self::POST: case self::PUT: $dataString = $this->encodeData($data); $context_options['http']['method'] = $method; $context_options['http']['content'] = $dataString; $context_options['http']['header'] .= 'Context-Length: ' . strlen($dataString) . "\r\n"; break; } //add options for the ssl connection if (\Everyman\Neo4j\DI::isRegistered("httpStreamOptions")) { $streamOptions = \Everyman\Neo4j\DI::resolve("httpStreamOptions"); if (is_array($streamOptions)) { $context_options = array_replace_recursive($context_options, $streamOptions); } unset($streamOptions); } $context = stream_context_create($context_options); $response = file_get_contents($url, false, $context); // $http_response_header is set by file_get_contents with the http:// wrapper preg_match('/^HTTP\\/1\\.[0-1] (\\d{3})/', $http_response_header[0], $parts); $code = $parts[1]; if (!$code) { $code = 500; $response = json_encode(array("error" => 'error [' . $code . ']')); } $bodyData = json_decode($response, true); $headers = array(); foreach ($http_response_header as $header) { $parts = explode(':', $header, 2); if (count($parts) == 2) { $headers[$parts[0]] = $parts[1]; } } return array('code' => $code, 'headers' => $headers, 'data' => $bodyData); }
<?php namespace Everyman\Neo4j; use Everyman\Neo4j\Transport as BaseTransport; //db part of the rest endpoint DI::register("dbPath", "/db/data"); //register node creation callback DI::register("Node", function (Client $client, $properties = array()) { return new Node($client); }); //register relationship creation callback DI::register("Relationship", function (Client $client, $properties = array()) { return new Relationship($client); }); //register transport creation callback DI::register("transport", function ($host = 'localhost', $port = 7474) { if (extension_loaded("curl")) { return new BaseTransport\Curl($host, $port); } return new BaseTransport\Stream($host, $port); }, true); //ensure consistency between the default ssl //varification with curl and with http stream DI::register("httpStreamOptions", array("ssl" => array("verify_peer" => true, "allow_self_signed" => false)));
/** * Resolves cache key by given type. Checks if there is registered * cache prefix for this type and returns it, otherwise it returns * the type itself as a key * @param string $type * @return string */ protected function resolveCacheKey($type) { if (DI::isRegistered($type . "CachePrefix")) { return DI::resolve($type . "CachePrefix"); } return $type; }