You are here

function HTTP_Request::_buildRequest in Flickr API 5

Builds the request string

@access private

Return value

string The request string

1 call to HTTP_Request::_buildRequest()
HTTP_Request::sendRequest in phpFlickr/PEAR/HTTP/Request.php
Sends the request

File

phpFlickr/PEAR/HTTP/Request.php, line 884

Class

HTTP_Request
Class for performing HTTP requests

Code

function _buildRequest() {
  $separator = ini_get('arg_separator.output');
  ini_set('arg_separator.output', '&');
  $querystring = ($querystring = $this->_url
    ->getQueryString()) ? '?' . $querystring : '';
  ini_set('arg_separator.output', $separator);
  $host = isset($this->_proxy_host) ? $this->_url->protocol . '://' . $this->_url->host : '';
  $port = (isset($this->_proxy_host) and $this->_url->port != 80) ? ':' . $this->_url->port : '';
  $path = $this->_url->path . $querystring;
  $url = $host . $port . $path;
  if (!strlen($url)) {
    $url = '/';
  }
  $request = $this->_method . ' ' . $url . ' HTTP/' . $this->_http . "\r\n";
  if (in_array($this->_method, $this->_bodyDisallowed) || 0 == strlen($this->_body) && (HTTP_REQUEST_METHOD_POST != $this->_method || empty($this->_postData) && empty($this->_postFiles))) {
    $this
      ->removeHeader('Content-Type');
  }
  else {
    if (empty($this->_requestHeaders['content-type'])) {

      // Add default content-type
      $this
        ->addHeader('Content-Type', 'application/x-www-form-urlencoded');
    }
    elseif ('multipart/form-data' == $this->_requestHeaders['content-type']) {
      $boundary = 'HTTP_Request_' . md5(uniqid('request') . microtime());
      $this
        ->addHeader('Content-Type', 'multipart/form-data; boundary=' . $boundary);
    }
  }

  // Request Headers
  if (!empty($this->_requestHeaders)) {
    foreach ($this->_requestHeaders as $name => $value) {
      $canonicalName = implode('-', array_map('ucfirst', explode('-', $name)));
      $request .= $canonicalName . ': ' . $value . "\r\n";
    }
  }

  // No post data or wrong method, so simply add a final CRLF
  if (in_array($this->_method, $this->_bodyDisallowed) || HTTP_REQUEST_METHOD_POST != $this->_method && 0 == strlen($this->_body)) {
    $request .= "\r\n";

    // Post data if it's an array
  }
  elseif (HTTP_REQUEST_METHOD_POST == $this->_method && (!empty($this->_postData) || !empty($this->_postFiles))) {

    // "normal" POST request
    if (!isset($boundary)) {
      $postdata = implode('&', array_map(create_function('$a', 'return $a[0] . \'=\' . $a[1];'), $this
        ->_flattenArray('', $this->_postData)));

      // multipart request, probably with file uploads
    }
    else {
      $postdata = '';
      if (!empty($this->_postData)) {
        $flatData = $this
          ->_flattenArray('', $this->_postData);
        foreach ($flatData as $item) {
          $postdata .= '--' . $boundary . "\r\n";
          $postdata .= 'Content-Disposition: form-data; name="' . $item[0] . '"';
          $postdata .= "\r\n\r\n" . urldecode($item[1]) . "\r\n";
        }
      }
      foreach ($this->_postFiles as $name => $value) {
        if (is_array($value['name'])) {
          $varname = $name . ($this->_useBrackets ? '[]' : '');
        }
        else {
          $varname = $name;
          $value['name'] = array(
            $value['name'],
          );
        }
        foreach ($value['name'] as $key => $filename) {
          $fp = fopen($filename, 'r');
          $data = fread($fp, filesize($filename));
          fclose($fp);
          $basename = basename($filename);
          $type = is_array($value['type']) ? @$value['type'][$key] : $value['type'];
          $postdata .= '--' . $boundary . "\r\n";
          $postdata .= 'Content-Disposition: form-data; name="' . $varname . '"; filename="' . $basename . '"';
          $postdata .= "\r\nContent-Type: " . $type;
          $postdata .= "\r\n\r\n" . $data . "\r\n";
        }
      }
      $postdata .= '--' . $boundary . "--\r\n";
    }
    $request .= 'Content-Length: ' . (HTTP_REQUEST_MBSTRING ? mb_strlen($postdata, 'iso-8859-1') : strlen($postdata)) . "\r\n\r\n";
    $request .= $postdata;

    // Explicitly set request body
  }
  elseif (0 < strlen($this->_body)) {
    $request .= 'Content-Length: ' . (HTTP_REQUEST_MBSTRING ? mb_strlen($this->_body, 'iso-8859-1') : strlen($this->_body)) . "\r\n\r\n";
    $request .= $this->_body;

    // Terminate headers with CRLF on POST request with no body, too
  }
  else {
    $request .= "\r\n";
  }
  return $request;
}