| | 1448 | if( ! function_exists('remote_http') ){ |
| | 1449 | /** |
| | 1450 | * remote_http() - makes a HTTP/1.0 request. |
| | 1451 | * |
| | 1452 | * Makes a remote HTTP request and returns a object repreenting the request. |
| | 1453 | * Please note that this function does not support Proxies, Is not cached, And times out on the connection attempt after 3 seconds. |
| | 1454 | * |
| | 1455 | * The headers sent by this function can be modified/added to by the 'remote_http_headers' filter. |
| | 1456 | * All headers returned by this function are normalised to lowercase, Content is not modified. |
| | 1457 | * |
| | 1458 | * @since 2.6 |
| | 1459 | * |
| | 1460 | * @param string $url the URL to open |
| | 1461 | * @param string $method (optional) the HTTP method for the connection, Accepts GET, POST or HEAD |
| | 1462 | * @param string|array $data (optional) an Associative array containing data keys to be sent, or a pre-made HTTP query string |
| | 1463 | * @param array $headers (optional) Extra headers to send with the connection. eg. array('Referer' => 'http://localhost/'); |
| | 1464 | * @return bool|object Returns false on failure, Else an object with 4 properties: "stauts", "statusname", "headers" and "content" |
| | 1465 | */ |
| | 1466 | function remote_http($url, $method='GET', $data='', $headers = array()){ |
| | 1467 | //Note: This returns all headers in lowercase. |
| | 1468 | global $wp_version; |
| | 1469 | |
| | 1470 | if( ! function_exists('fsockopen') ) |
| | 1471 | return false; |
| | 1472 | |
| | 1473 | if( is_array($data) ) |
| | 1474 | $data = http_build_query($data); |
| | 1475 | |
| | 1476 | if( ! is_array($headers) ) |
| | 1477 | $headers = array(); |
| | 1478 | |
| | 1479 | $allowed_methods = array('GET', 'POST', 'HEAD'); |
| | 1480 | if( ! in_array($method, $allowed_methods) ) |
| | 1481 | $method = 'GET'; |
| | 1482 | |
| | 1483 | $port = 80; |
| | 1484 | $schema = 'http'; |
| | 1485 | $path = $host = $query = ''; |
| | 1486 | $parts = parse_url($url); |
| | 1487 | extract($parts); |
| | 1488 | |
| | 1489 | if( ! empty($query) ) |
| | 1490 | $query = "?$query"; |
| | 1491 | |
| | 1492 | $http_request = "$method $path$query HTTP/1.0\r\n"; |
| | 1493 | $http_request .= "Host: $host\r\n"; |
| | 1494 | |
| | 1495 | if( ! isset($headers['User-Agent']) ) |
| | 1496 | $headers['User-Agent'] = 'WordPress/' . $wp_version . '; ' . get_bloginfo('url'); |
| | 1497 | |
| | 1498 | if( ! empty($data) ) { |
| | 1499 | $headers['Content-Length'] = strlen($data); |
| | 1500 | |
| | 1501 | if( ! isset($headers['Content-Type']) ) |
| | 1502 | $headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=' . get_option('blog_charset'); |
| | 1503 | } |
| | 1504 | |
| | 1505 | $headers = apply_filters('remote_http_headers', $headers); |
| | 1506 | |
| | 1507 | if( ! empty($headers) ) |
| | 1508 | foreach($headers as $header => $value) |
| | 1509 | $http_request .= "$header: $value\r\n"; |
| | 1510 | |
| | 1511 | $http_request .= "\r\n"; |
| | 1512 | if( ! empty($data) ) |
| | 1513 | $http_request .= $data; |
| | 1514 | |
| | 1515 | $response = ''; |
| | 1516 | if( false == ( $fs = @fsockopen( $host, $port, $errno, $errstr, 3) ) || ! is_resource($fs) ) |
| | 1517 | return false; |
| | 1518 | |
| | 1519 | fwrite($fs, $http_request); |
| | 1520 | |
| | 1521 | while ( !feof($fs) ) |
| | 1522 | $response .= fgets($fs, 1160); // One TCP-IP packet |
| | 1523 | fclose($fs); |
| | 1524 | $response = explode("\r\n\r\n", $response, 2); |
| | 1525 | |
| | 1526 | $ret = (object)array('status' => 0, 'statusvalue' => '', 'headers' => array(), 'content'=> $response[1]); |
| | 1527 | |
| | 1528 | foreach( explode("\n", $response[0]) as $rheader) { |
| | 1529 | if( strpos($rheader, ':') ) { //Proper header |
| | 1530 | list($header, $value) = explode(':', $rheader, 2); |
| | 1531 | if( !empty($header) && ! empty($value) ) |
| | 1532 | $ret->headers[ strtolower(trim($header)) ] = trim($value); |
| | 1533 | } else if( strpos($rheader, 'HTTP') > -1) { //A Status header. |
| | 1534 | list(,$status, $status_value) = preg_split('|\s+|', $rheader,3); |
| | 1535 | $ret->status = trim($status); |
| | 1536 | $ret->statusvalue = trim($status_value); |
| | 1537 | } |
| | 1538 | } |
| | 1539 | |
| | 1540 | return $ret; |
| | 1541 | } |
| | 1542 | } |