コード例 #1
0
ファイル: Client.php プロジェクト: catlabinteractive/neuron
 private function api(Request $request, $method)
 {
     $ch = curl_init();
     $post = $request->getBody();
     $parsedUrl = $request->getUrl();
     if ($request->getParameters()) {
         if (strpos($parsedUrl, '?')) {
             $parsedUrl .= '&';
         } else {
             $parsedUrl .= '?';
         }
         $parsedUrl .= http_build_query($request->getParameters());
     }
     curl_setopt($ch, CURLOPT_URL, $parsedUrl);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_HEADER, 1);
     if ($request->getHeaders()) {
         $headers = $request->getHeaders();
         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     }
     switch ($method) {
         case 'GET':
             break;
         case 'POST':
             curl_setopt($ch, CURLOPT_POST, 1);
             curl_setopt($ch, CURLOPT_POST, 1);
             curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
             break;
         case 'DELETE':
             throw new NotImplemented("Not implemented.");
             break;
         case 'PUT':
             curl_setopt($ch, CURLOPT_PUT, 1);
             curl_setopt($ch, CURLOPT_POST, 1);
             curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
             break;
     }
     $output = curl_exec($ch);
     // Response
     $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
     $header = substr($output, 0, $header_size);
     $body = substr($output, $header_size);
     $response = Response::fromRaw($body, self::http_parse_headers($header));
     curl_close($ch);
     return $response;
 }