Ignore:
Timestamp:
Dec 11, 2010, 8:50:05 PM (13 years ago)
Author:
patdenice
Message:

feature 2057: fetchRemote can send POST data

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/include/functions.php

    r8022 r8079  
    17031703 * @return bool
    17041704 */
    1705 function fetchRemote($src, &$dest, $user_agent='Piwigo', $step=0)
     1705function fetchRemote($src, &$dest, $get_data=array(), $post_data=array(), $user_agent='Piwigo', $step=0)
    17061706{
    17071707  // Try to retrieve data from local file?
     
    17191719    }
    17201720  }
     1721
     1722  // After 3 redirections, return false
     1723  if ($step > 3) return false;
     1724
     1725  // Initialization
     1726  $method  = empty($post_data) ? 'GET' : 'POST';
     1727  $request = empty($post_data) ? '' : http_build_query($post_data, '', '&');
     1728  $src     = add_url_params($src, $get_data, '&');
    17211729
    17221730  // Send anonymous data to piwigo server
     
    17381746  }
    17391747
    1740   // After 3 redirections, return false
    1741   if ($step > 3) return false;
    1742 
    17431748  // Initialize $dest
    17441749  is_resource($dest) or $dest = '';
     
    17521757    @curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
    17531758    @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     1759    if ($method == 'POST')
     1760    {
     1761      @curl_setopt($ch, CURLOPT_POST, 1);
     1762      @curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
     1763    }
    17541764    $content = @curl_exec($ch);
    17551765    $header_length = @curl_getinfo($ch, CURLINFO_HEADER_SIZE);
     
    17601770      if (preg_match('/Location:\s+?(.+)/', substr($content, 0, $header_length), $m))
    17611771      {
    1762         return fetchRemote($m[1], $dest, $user_agent, $step+1);
     1772        return fetchRemote($m[1], $dest, array(), array(), $user_agent, $step+1);
    17631773      }
    17641774      $content = substr($content, $header_length);
     
    17711781  if (ini_get('allow_url_fopen'))
    17721782  {
    1773     $content = @file_get_contents($src);
     1783    $opts = array(
     1784      'http' => array(
     1785        'method' => $method,
     1786        'content' => $request,
     1787        'user_agent' => $user_agent,
     1788      )
     1789    );
     1790    $context = @stream_context_create($opts);
     1791    $content = @file_get_contents($src, false, $context);
    17741792    if ($content !== false)
    17751793    {
     
    17901808  }
    17911809
    1792   fwrite($s,
    1793     "GET ".$path." HTTP/1.0\r\n"
    1794     ."Host: ".$host."\r\n"
    1795     ."User-Agent: ".$user_agent."\r\n"
    1796     ."Accept: */*\r\n"
    1797     ."\r\n"
    1798   );
     1810  $http_request  = $method." ".$path." HTTP/1.0\r\n";
     1811  $http_request .= "Host: ".$host."\r\n";
     1812  $http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
     1813  $http_request .= "Content-Length: ".strlen($request)."\r\n";
     1814  $http_request .= "User-Agent: ".$user_agent."\r\n";
     1815  $http_request .= "Accept: */*\r\n";
     1816  $http_request .= "\r\n";
     1817  $http_request .= $request;
     1818
     1819  fwrite($s, $http_request);
    17991820
    18001821  $i = 0;
     
    18291850      {
    18301851        fclose($s);
    1831         return fetchRemote(trim($m[1]),$dest,$user_agent,$step+1);
     1852        return fetchRemote(trim($m[1]),$dest,array(),array(),$user_agent,$step+1);
    18321853      }
    18331854      $i++;
Note: See TracChangeset for help on using the changeset viewer.