/** * Write data to the response body of a request * * @param resource $curl * @param string $write * * @return int */ public function writeResponseBody($curl, $write) { if (!($response = $this->transaction->getResponse())) { return 0; } // Add a default body on the response if one was not found if (!($body = $response->getBody())) { $body = new puzzle_stream_Stream(fopen('php://temp', 'r+')); $response->setBody($body); } return $body->write($write); }
/** * Emits the complete event for a request and emits an error * event if an error is encountered during the after send. * * @param puzzle_adapter_TransactionInterface $transaction Transaction to emit for * @param array $stats Transfer stats * * @throws puzzle_exception_RequestException */ public static function emitComplete(puzzle_adapter_TransactionInterface $transaction, array $stats = array()) { $request = $transaction->getRequest(); $transaction->getResponse()->setEffectiveUrl($request->getUrl()); try { $request->getEmitter()->emit('complete', new puzzle_event_CompleteEvent($transaction, $stats)); } catch (puzzle_exception_RequestException $e) { self::emitError($transaction, $e, $stats); } }
private function validateResponseWasSet(puzzle_adapter_TransactionInterface $transaction, puzzle_adapter_curl_BatchContext $context) { if ($transaction->getResponse()) { return true; } $body = $transaction->getRequest()->getBody(); if (!$body) { // This is weird and should probably never happen. puzzle_event_RequestEvents::emitError($transaction, new puzzle_exception_RequestException('No response was received for a request with no body. This' . ' could mean that you are saturating your network.', $transaction->getRequest())); } elseif (!$body->isSeekable() || !$body->seek(0)) { // Nothing we can do with this. Sorry! puzzle_event_RequestEvents::emitError($transaction, new puzzle_exception_RequestException('The connection was unexpectedly closed. The request would' . ' have been retried, but attempting to rewind the' . ' request body failed. Consider wrapping your request' . ' body in a CachingStream decorator to work around this' . ' issue if necessary.', $transaction->getRequest())); } else { $this->retryFailedConnection($transaction, $context); } return false; }