{"id":5919,"date":"2014-04-10T07:47:28","date_gmt":"2014-04-10T07:47:28","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/10\/google-sitemap-ping-success-collection-of-common-programming-errors-2\/"},"modified":"2014-04-10T07:47:28","modified_gmt":"2014-04-10T07:47:28","slug":"google-sitemap-ping-success-collection-of-common-programming-errors-2","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/10\/google-sitemap-ping-success-collection-of-common-programming-errors-2\/","title":{"rendered":"Google Sitemap Ping Success-Collection of common programming errors"},"content":{"rendered":"<p>Since commands like <code>shell_exec()<\/code>, <code>exec()<\/code>, <code>passthru()<\/code> etc. are blocked by many hosters, you should use <code>curl<\/code> and check for a response code of 200.<\/p>\n<p>You could also use <code>fsockopen<\/code> if curl is not available. I&#8217;m going to check for the code snippet and update the answer when I found it.<\/p>\n<p><strong>UPDATE<\/strong>:<\/p>\n<p>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.<\/p>\n<p>Either way, here is the code for stand alone implementation.<\/p>\n<pre><code>\/**\n * Call url with fsockopen and return the response status.\n *\n * @param string $url\n *  The url to call.\n *\n * @return mixed(boolean|int)\n *  The http status code of the response. FALSE if something went wrong.\n*\/\nfunction _callWithFSockOpen($url) {\n    $result = FALSE;\n\n    \/\/ Parse url.\n    $url = parse_url($url);\n    \/\/ Append query to path.\n    $url['path'] .= '?'.$url['query'];\n\n    \/\/ Setup fsockopen.\n    $port = 80;\n    $timeout = 10;\n    $fso = fsockopen($url['host'], $port, $errno, $errstr, $timeout);\n\n    \/\/ Proceed if connection was successfully opened.\n    if ($fso) {\n        \/\/ Create headers.\n        $headers = 'GET ' . $url['path'] . 'HTTP\/1.0' . \"\\r\\n\";\n        $headers .= 'Host: ' . $url['host'] . \"\\r\\n\";\n        $headers .= 'Connection: closed' . \"\\r\\n\";\n        $headers .= \"\\r\\n\";\n\n        \/\/ Write headers to socket.\n        fwrite($fso, $headers);\n\n        \/\/ Set timeout for stream read\/write.\n        stream_set_timeout($fso, $timeout);\n\n        \/\/ Use a loop in case something unexpected happens.\n        \/\/ I do not know what, but that why it is unexpected.           \n        while (!feof($fso)){\n            \/\/ 128 bytes is getting the header with the http response code in it.               \n            $buffer = fread($fso, 128);\n\n            \/\/ Filter only the http status line (first line) and break loop on success.\n            if(!empty($buffer) &amp;&amp; ($buffer = substr($buffer, 0, strpos($buffer, \"\\r\\n\")))){\n                break;\n            }\n        }\n\n        \/\/ Match status.\n        preg_match('\/^HTTP.+\\s(\\d{3})\/', $buffer, $match);\n        \/\/ Extract status.\n        list(, $status) = $match;\n\n        $result = $status;\n    }\n    else {\n        \/\/ @XXX: Throw exception here??\n    }\n\n    return (int) $result;\n}\n<\/code><\/pre>\n<p>If you guys find any harm or improvement in this code, do not hesitate to open up a ticket\/pull request on GitHub, please. \ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;m going to check for the code snippet and update the answer when I found it. UPDATE: Found it. I knew [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5919","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5919","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5919"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5919\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}