-
Code's Tags
-
Your Codes
-
Reffers
-
Linked Codes
|
Code:
Short link for Twitter:
HTML:
HTML view:
Copy Source | Copy HTML- // sockets version HTTP/POST
- function http_post( $url, $data ) {
- $eol = "\r\n";
- $post = '';
- if (is_array($data)) {
- foreach( $data as $k => $v)
- $post .= $k.'='.urlencode($v).'&';
- $post = substr($post, 0,-1);
- $content_type = 'application/x-www-form-urlencoded';
- } else {
- $post = $data;
- if (strpos($post, '<?xml') === 0)
- $content_type = 'text/xml';
- else if (strpos($post, '{') === 0)
- $content_type = 'application/json';
- else
- $content_type = 'text/html';
- }
- if ((($u = parse_url($url)) === false) || !isset($u['host'])) return false;
- if (!isset($u['scheme'])) $u['scheme'] = 'http';
- $request = 'POST '.(isset($u['path']) ? $u['path'] : '/').((isset($u['query'])) ? '?'.$u['query'] : '' ).' HTTP/1.1'.$eol
- .'Host: '.$u['host'].$eol
- .'Content-Type: '.$content_type.$eol
- .'Content-Length: '.mb_strlen($post, 'latin1').$eol
- .'Connection: close'.$eol.$eol
- .$post;
- $host = ($u['scheme'] == 'https') ? 'ssl://'.$u['host'] : $u['host'];
- if (isset($u['port']))
- $port = $u['port'];
- else
- $port = ($u['scheme'] == 'https') ? 443 : 80;
- $fp = @fsockopen( $host, $port, $errno, $errstr, 10);
- if ($fp) {
- $content = '';
- $content_length = false;
- $chunked = false;
- fwrite($fp, $request);
- // read headers
- while ($line = fgets($fp)) {
- if (preg_match('~Content-Length: (\d+)~i', $line, $matches)) {
- $content_length = (int) $matches[1];
- } else if (preg_match('~Transfer-Encoding: chunked~i', $line)) {
- $chunked = true;
- } else if ($line == "\r\n") {
- break;
- }
- }
- // read content
- if ($content_length !== false) {
- $_size = 4096;
- do {
- $_data = fread($fp, $_size );
- $content .= $_data;
- $_size = min($content_length-strlen($content), 4096);
- } while( $_size > 0 );
- } else if ($chunked) {
- while ( $chunk_length = hexdec(trim(fgets($fp))) ) {
- $chunk = '';
- $read_length = 0;
- while ( $read_length < $chunk_length ) {
- $chunk .= fread($fp, $chunk_length - $read_length);
- $read_length = strlen($chunk);
- }
- $content .= $chunk;
- fgets($fp);
- }
- } else {
- while(!feof($fp)) $content .= fread($fp, 4096);
- }
- fclose($fp);
- // echo $content;
- return $content;
- } else {
- return false;
- }
- }
|