/** * Returns a response for specified HTTP request. * * @param Request $request HTTP Request to send. * * @return Response Response for specified request. */ public function send(Request $request) { $ch = curl_init($request->getUrl()); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request->getMethod()); curl_setopt($ch, CURLOPT_HTTPHEADER, HttpUtil::formatHeadersForCurl($request->getHeaders())); if (!is_null($request->getBody())) { curl_setopt($ch, CURLOPT_POSTFIELDS, $request->getBody()); } curl_setopt_array($ch, $request->getCurlOptions()); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FAILONERROR, false); curl_setopt($ch, CURLOPT_HEADER, true); list($status, $headers, $body) = HttpUtil::parseResponse(curl_exec($ch)); return new Response(HttpUtil::parseStatus($status), HttpUtil::parseHeaders($headers), $body, curl_getinfo($ch)); }
/** * Makes sure we've properly handled the POST body, such as ensuring that * CURLOPT_INFILESIZE is set if CURLOPT_READFUNCTION is set. * * @param Request $request Request to set cURL option to. * @param resource $curlHandle cURL handle associated with the request. */ public static function validateCurlPOSTBody(Request $request, $curlHandle = null) { $readFunction = $request->getCurlOption(CURLOPT_READFUNCTION); if (is_null($readFunction)) { return; } // Guzzle 4 sometimes sets the post body in CURLOPT_POSTFIELDS even if // they have already set CURLOPT_READFUNCTION. if ($request->getBody()) { return; } $bodySize = $request->getCurlOption(CURLOPT_INFILESIZE); Assertion::notEmpty($bodySize, "To set a CURLOPT_READFUNCTION, CURLOPT_INFILESIZE must be set."); $body = call_user_func_array($readFunction, array($curlHandle, fopen('php://memory', 'r'), $bodySize)); $request->setBody($body); }