/** * * creates a curl resource with the parameters provided by the {@link Request} object * * @param Request $request * @return curl resource */ private function getCurlInstance(Request $request) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $request->buildUrl(true, $request->getMethod() == 'GET')); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Accept-Charset: utf-8')); if ($request->getUsername() && $request->getPassword()) { curl_setopt($ch, CURLOPT_USERPWD, $request->getUsername() . ':' . $request->getPassword()); } return $ch; }
public function execute(Request $request) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $request->getUrl()); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); $username = $request->getUsername(); $password = $request->getPassword(); if ($username && $password) { curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password); } switch ($request->getMethod()) { case self::POST: case self::PUT: curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($request->getParameters())); break; case self::DELETE: curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); break; case self::GET: default: break; } $result = curl_exec($ch); if (!$result) { $errorNumber = curl_errno($ch); $error = curl_error($ch); curl_close($ch); throw new \Exception($errorNumer . ': ' . $error); } curl_close($ch); return $request->getResponseTransformerImpl()->transform($result); }