{"id":2719,"date":"2009-01-26T21:09:57","date_gmt":"2009-01-27T05:09:57","guid":{"rendered":"http:\/\/wapreview.com\/?p=2719"},"modified":"2009-01-27T20:44:59","modified_gmt":"2009-01-28T04:44:59","slug":"mobile-adsense-and-fopen","status":"publish","type":"post","link":"https:\/\/wapreview.com\/2719\/","title":{"rendered":"AdSense for Mobile Woes – Solved!"},"content":{"rendered":"

\"AdSenseRemember a month ago when I blogged<\/a> about how AdSense for Mobile<\/em> had suddenly stopped working on wapreview.mobi.\u00a0 And my repeated attempts at contacting Google using their official support email and online help form and getting nothing in reply except unhelpful automated replies?<\/p>\n

Well I finally got AdSense working again, no thanks to Google and their apparent policy of no support for AdSense.\u00a0 To be fair, the problem was never Google’s.\u00a0 It turned out that something I did broke AdSense.\u00a0 That something was upgrading from PHP4 to PHP5!\u00a0\u00a0 I wanted to use htmLawed<\/a>, a\u00a0 library that requires PHP5, in the new mobile version<\/a> of the WapReview Directory. Although I made no changes to the Google AdSense code, the simple act of upgrading PHP caused AdSense to stop working.\u00a0 That’s because\u00a0 I use\u00a0 1and1 shared hosting and 1and1 has set the allow_url_fopen<\/code> flag in php.ini to false for PHP5 and true for PHP4. Google uses fopen() to retrieve ads from a remote server in their domain.\u00a0 There are some real security issues<\/a> around using fopen() particularly if you use it with\u00a0 user specified urls. The way Google uses fopen() with a single hard-coded url is perfectly safe.\u00a0 Still I can’t blame 1and1 for disabling the functionality as allowing fopen() with remote urls\u00a0 is a security risk in a shared hosting environment.\u00a0 You’d\u00a0 think they would be consistent though and disable allow_url_fopen on both supported PHP versions.<\/p>\n

If you are using AdSense for Mobile with 1and1 or another host that has disabled allow_url_fopen you can probably get AdSense to work with a simple modification to the Google supplied PHP code.\u00a0 You need to replace the fopen() call with calls to cURL functions. Look for the following in your AdSense code\u00a0 (it’s at the very bottom of the Google supplied code).<\/p>\n

$google_ad_handle = @fopen(google_get_ad_url(), 'r');\r\nif ($google_ad_handle) {\r\n  while (!feof($google_ad_handle)) {\r\n    echo fread($google_ad_handle, 8192);\r\n  }\r\n  fclose($google_ad_handle);\r\n}\r\n?><\/span><\/pre>\n

Replace the above\u00a0 code snippet with:<\/p>\n

function curl_get_contents($url, $timeout=60) {\r\n  $timeout = abs((int)$timeout);\r\n  $curl = curl_init();\r\n  $opts = array(\r\n    CURLOPT_URL => $url,\r\n    CURLOPT_RETURNTRANSFER => true,\r\n    CURLOPT_TIMEOUT => $timeout\r\n  );\r\n\r\n  curl_setopt_array($curl, $opts);\r\n  $result = curl_exec($curl);\r\n  if($result === false){\r\n    user_error(curl_error($curl));\r\n    return false;\r\n  }\r\n  $response = curl_getinfo($curl, CURLINFO_HTTP_CODE);\r\n  if($response != 200){\r\n    user_error(\"Received HTTP response of $response.\");\r\n  }\r\n  return $result;\r\n}\r\n\r\n$url= google_get_ad_url();\r\n$contents = curl_get_contents($url);\r\necho $contents;\r\n?><\/span><\/pre>\n

The curl_get_contents() function used above is from user NogDog<\/a> at the PHPBuilder.com<\/a> forum.<\/p>\n

Disabling the remote url functionality is apparently quite common in shared hosting environments.\u00a0 So why does Google persist in using it in the PHP code?\u00a0 Simple, the cURL library is not enabled by default\u00a0 in any PHP version.\u00a0 I don’t have a clue why not as it’s the preferred, safer way to open remote files.\u00a0 Because curl is an optional PHP extension it really isn’t possible to provide a single code snippet that works in all environments.\u00a0 Still it would be nice of Google to warn users about this potential issue and offer an alternate version of their code using cURL .\u00a0 That’s\u00a0 what AdMob does.\u00a0 They provide two versions of their PHP code; one using fopen() and the other cURL. AdMob recomends trying the cURL code first and if that doesn’t work then using the fopen() variant.<\/p>\n","protected":false},"excerpt":{"rendered":"

Remember a month ago when I blogged about how AdSense for Mobile had suddenly stopped working on wapreview.mobi.\u00a0 And my repeated attempts at contacting Google using their official support email and online help form and getting nothing in reply except unhelpful automated replies? Well I finally got AdSense working again, no thanks to Google and their apparent policy of no support for AdSense.\u00a0 To be fair, the problem was never Google’s.\u00a0 It turned out that something I did broke AdSense.\u00a0 … Continue reading →<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":14202,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[51],"tags":[560,586,369],"_links":{"self":[{"href":"https:\/\/wapreview.com\/wp-json\/wp\/v2\/posts\/2719"}],"collection":[{"href":"https:\/\/wapreview.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wapreview.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wapreview.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/wapreview.com\/wp-json\/wp\/v2\/comments?post=2719"}],"version-history":[{"count":18,"href":"https:\/\/wapreview.com\/wp-json\/wp\/v2\/posts\/2719\/revisions"}],"predecessor-version":[{"id":2734,"href":"https:\/\/wapreview.com\/wp-json\/wp\/v2\/posts\/2719\/revisions\/2734"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wapreview.com\/wp-json\/wp\/v2\/media\/14202"}],"wp:attachment":[{"href":"https:\/\/wapreview.com\/wp-json\/wp\/v2\/media?parent=2719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wapreview.com\/wp-json\/wp\/v2\/categories?post=2719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wapreview.com\/wp-json\/wp\/v2\/tags?post=2719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}