コード例 #1
0
ファイル: stringify.php プロジェクト: sebbie42/casebox
/**
 * This simple example shows the capability of Request and Response objects to
 * serialize themselves as strings.
 *
 * This is mainly useful for debugging purposes.
 *
 * @copyright Copyright (C) 2009-2015 fruux GmbH (https://fruux.com/).
 * @author Evert Pot (http://evertpot.com/)
 * @license http://sabre.io/license/ Modified BSD License
 */
use Sabre\HTTP\Request;
use Sabre\HTTP\Response;
// Find the autoloader
$paths = [__DIR__ . '/../vendor/autoload.php', __DIR__ . '/../../../autoload.php', __DIR__ . '/vendor/autoload.php'];
foreach ($paths as $path) {
    if (file_exists($path)) {
        include $path;
        break;
    }
}
$request = new Request('POST', '/foo');
$request->setHeaders(['Host' => 'example.org', 'Content-Type' => 'application/json']);
$request->setBody(json_encode(['foo' => 'bar']));
echo $request;
echo "\r\n\r\n";
$response = new Response(424);
$response->setHeaders(['Content-Type' => 'text/plain', 'Connection' => 'close']);
$response->setBody("ABORT! ABORT!");
echo $response;
echo "\r\n";
コード例 #2
0
ファイル: Client.php プロジェクト: LukasReschke/sabre-http
 /**
  * Parses the result of a curl call in a format that's a bit more
  * convenient to work with.
  *
  * The method returns an array with the following elements:
  *   * status - one of the 3 STATUS constants.
  *   * curl_errno - A curl error number. Only set if status is
  *                  STATUS_CURLERROR.
  *   * curl_errmsg - A current error message. Only set if status is
  *                   STATUS_CURLERROR.
  *   * response - Response object. Only set if status is STATUS_SUCCESS, or
  *                STATUS_HTTPERROR.
  *   * http_code - HTTP status code, as an int. Only set if Only set if
  *                 status is STATUS_SUCCESS, or STATUS_HTTPERROR
  *
  * @param string $response
  * @param resource $curlHandle
  * @return Response
  */
 protected function parseCurlResult($response, $curlHandle)
 {
     list($curlInfo, $curlErrNo, $curlErrMsg) = $this->curlStuff($curlHandle);
     if ($curlErrNo) {
         return ['status' => self::STATUS_CURLERROR, 'curl_errno' => $curlErrNo, 'curl_errmsg' => $curlErrMsg];
     }
     $headerBlob = substr($response, 0, $curlInfo['header_size']);
     // In the case of 204 No Content, strlen($response) == $curlInfo['header_size].
     // This will cause substr($response, $curlInfo['header_size']) return FALSE instead of NULL
     // An exception will be thrown when calling getBodyAsString then
     $responseBody = substr($response, $curlInfo['header_size']) ?: null;
     unset($response);
     // In the case of 100 Continue, or redirects we'll have multiple lists
     //
     // of headers for each separate HTTP response. We can easily split this
     // because they are separated by \r\n\r\n
     $headerBlob = explode("\r\n\r\n", trim($headerBlob, "\r\n"));
     // We only care about the last set of headers
     $headerBlob = $headerBlob[count($headerBlob) - 1];
     // Splitting headers
     $headerBlob = explode("\r\n", $headerBlob);
     $headers = array();
     foreach ($headerBlob as $header) {
         $parts = explode(':', $header, 2);
         if (count($parts) == 2) {
             $headers[trim($parts[0])] = trim($parts[1]);
         }
     }
     $response = new Response();
     $response->setStatus($curlInfo['http_code']);
     $response->setHeaders($headers);
     $response->setBody($responseBody);
     $httpCode = intval($response->getStatus());
     return ['status' => $httpCode >= 400 ? self::STATUS_HTTPERROR : self::STATUS_SUCCESS, 'response' => $response, 'http_code' => $httpCode];
 }