Google Sitemap Ping Success-Collection of common programming errors

Since commands like shell_exec(), exec(), passthru() etc. are blocked by many hosters, you should use curl and check for a response code of 200.

You could also use fsockopen if curl is not available. I’m going to check for the code snippet and update the answer when I found it.

UPDATE:

Found it. I knew I used it somewhere. The funny coincedence: It was in my Sitemap class xD You can find it here on github: https://github.com/func0der/Sitemap. It is in the Sitemap\SitemapOrg class. There is a also an example for the curl call implemented.

Either way, here is the code for stand alone implementation.

/**
 * Call url with fsockopen and return the response status.
 *
 * @param string $url
 *  The url to call.
 *
 * @return mixed(boolean|int)
 *  The http status code of the response. FALSE if something went wrong.
*/
function _callWithFSockOpen($url) {
    $result = FALSE;

    // Parse url.
    $url = parse_url($url);
    // Append query to path.
    $url['path'] .= '?'.$url['query'];

    // Setup fsockopen.
    $port = 80;
    $timeout = 10;
    $fso = fsockopen($url['host'], $port, $errno, $errstr, $timeout);

    // Proceed if connection was successfully opened.
    if ($fso) {
        // Create headers.
        $headers = 'GET ' . $url['path'] . 'HTTP/1.0' . "\r\n";
        $headers .= 'Host: ' . $url['host'] . "\r\n";
        $headers .= 'Connection: closed' . "\r\n";
        $headers .= "\r\n";

        // Write headers to socket.
        fwrite($fso, $headers);

        // Set timeout for stream read/write.
        stream_set_timeout($fso, $timeout);

        // Use a loop in case something unexpected happens.
        // I do not know what, but that why it is unexpected.           
        while (!feof($fso)){
            // 128 bytes is getting the header with the http response code in it.               
            $buffer = fread($fso, 128);

            // Filter only the http status line (first line) and break loop on success.
            if(!empty($buffer) && ($buffer = substr($buffer, 0, strpos($buffer, "\r\n")))){
                break;
            }
        }

        // Match status.
        preg_match('/^HTTP.+\s(\d{3})/', $buffer, $match);
        // Extract status.
        list(, $status) = $match;

        $result = $status;
    }
    else {
        // @XXX: Throw exception here??
    }

    return (int) $result;
}

If you guys find any harm or improvement in this code, do not hesitate to open up a ticket/pull request on GitHub, please. 😉