com web/php: use streams instead of raw sockets. solves the problem that the old implementation doesn't support ssl: include/posttohost.inc

From: Date: Sat, 28 Jul 2012 01:30:50 +0000
Subject: com web/php: use streams instead of raw sockets. solves the problem that the old implementation doesn't support ssl: include/posttohost.inc
Groups: php.webmaster 
Request: Send a blank email to php-webmaster+get-14019@lists.php.net to get a copy of this message
Commit: ac801d89cc224ed19ff44d3672110a49922e1b22 Author: Ferenc Kovacs <tyra3l@gmail.com> Sat, 28 Jul 2012 03:30:50 +0200 Parents: 1e75e669f2806fe2f0e6557fa01d55fe94d4144d Branches: master Link: http://git.php.net/?p=web/php.git;a=commitdiff;h=ac801d89cc224ed19ff44d3672110a49922e1b22 Log: use streams instead of raw sockets. solves the problem that the old implementation doesn't support ssl Changed paths: M include/posttohost.inc Diff: diff --git a/include/posttohost.inc b/include/posttohost.inc index 340ecb0..1e37d5b 100644 --- a/include/posttohost.inc +++ b/include/posttohost.inc @@ -10,50 +10,17 @@ function posttohost($url, $data) { - // Get parts of URL - $url = parse_url($url); - if (!$url) { return "couldn't parse url"; } - - // Provide defaults for port and query string - if (!isset($url['port'])) { $url['port'] = ""; } - if (!isset($url['query'])) { $url['query'] = ""; } + $data = http_build_query($data); - // Build POST string - $encoded = ""; - foreach ($data as $k => $v) { - $encoded .= ($encoded ? "&" : ""); - $encoded .= rawurlencode($k) . "=" . rawurlencode($v); - } + $opts = array( + 'method' => 'POST', + 'header' => 'Content-type: application/x-www-form-urlencoded', + 'content' => $data, + ); - // Open socket on host - $fp = @fsockopen($url['host'], $url['port'] ? $url['port'] : 80); - if (!$fp) { return "failed to open socket to {$url['host']}"; } + $ctx = stream_context_create(array('http' => $opts)); - // Send HTTP 1.0 POST request to host - fputs($fp, sprintf("POST %s%s%s HTTP/1.0\n", $url['path'], $url['query'] ? "?" : "", $url['query'])); - fputs($fp, "Host: {$url['host']}\n"); - fputs($fp, "Content-type: application/x-www-form-urlencoded\n"); - fputs($fp, "Content-length: " . strlen($encoded) . "\n"); - fputs($fp, "Connection: close\n\n"); - fputs($fp, "$encoded\n"); + $response_body = @file_get_contents($url, false, $ctx); - // Read the first line of data, only accept if 200 OK is sent - $line = fgets($fp, 1024); - if (!preg_match('#^HTTP/1\\.. 200#', $line)) { return; } - - // Put everything, except the headers to $results - $results = ""; $inheader = TRUE; - while(!feof($fp)) { - $line = fgets($fp, 1024); - if ($inheader && ($line == "\n" || $line == "\r\n")) { - $inheader = FALSE; - } - elseif (!$inheader) { - $results .= $line; - } - } - fclose($fp); - - // Return with data received - return $results; + return $response_body; }

« previous php.webmaster (#14019) next »