Functions
WebDev/*nix assorted functions & hacks
Search
find all
Array
-
Bash
-
Classes
-
Config
-
DateTime
-
Debug
-
dotjs
-
File
-
foooo
-
Git
-
HTML
-
Images
-
Javascript
-
Linux
-
Math
-
Misc
-
MySQL
-
OS X
-
Reading
-
Redis
-
Shell scripts
-
snippets
-
SQL
-
Strings
-
TextMate
-
Web
-
\(^o^)/ Fun!
-
_Misc hacks
-
_Pages
Web
curl_get($url, $raw=false)
httppost($url, $data)
http_build_query($array) & http_raw_query
http_raw_query($formdata, ..)
4
found
auth
Name:
Category: Web
Body:
// I was totally out of function names. // This fetches a webpage and returns it as an array of status (nested array), assoc headers and body. // Or as a big fat string for $raw: True // I tried to make it RFC 2616 compliant mostly, but the header keys are made lowercase (for ease of use) //_____________________________ // curl_get($url, $raw=false) / function curl_get($url, $raw=false) { if (! $url) return false; $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 1); // include headers to output curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // have curl_exec return output as string $string = curl_exec($ch); curl_close($ch); if ($raw) return $string; // process more, split headers from body $gap_pos = strpos($string, "\r\n\r\n"); $header = substr($string, 0, $gap_pos); $body = substr($string, $gap_pos+4); // status line $status_line = substr($header, 0, strpos($header, "\r\n")); list($status_http, $status_code, $status_text) = explode(' ', $status_line, 3); // headers $keys = array(); foreach(explode("\r\n", substr($header, strpos($header, "\r\n")+2)) as $line) { if (strpos($line, ':') > 0) { $col_pos = strpos($line, ':'); list($key, $value) = array(strtolower(trim(substr($line, 0, $col_pos))), trim(substr($line, $col_pos+1))); if (in_array($key, $keys)) { $headers[$key] .= ', '.$value; // as per RFC 2616 sec 4.2, 2.1 }else{ $headers[$key] = $value; } $keys[] = $key; }else{ $malformed_headers[] = $line; } } $out = array( 'status' => array( 'line' => $status_line, 'http' => $status_http, 'code' => $status_code, 'text' => $status_text ), 'headers' => $headers, 'body' => $body ); if (isset($malformed_headers)) $out['errors']['malformed_headers'] = $malformed_headers; return $out; }
Footer