-2

I have a PHP website with mobile version and google ads are displayed there. I have received google code from my client for the header and the footer, respectively, and they are not always displaying. I receive a lot of warnings in the automatic error reports looking like this:

fopen(http://pagead2.googlesyndication.com/pagead/ads?...): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

After the question mark in the URL there are some parameters with private information, so I decided to write ... instead of them. I have asked my client to regenerate the codes and he has done so, I have pasted the new code for the header and the footer too, however, when we thought the error is gone we decided to call it a day and after 16 hours I have received 4352 error reports, 95% of them was with google ads, the error being the same, namely: failed to open stream. I wonder why do we see sometimes the ads and why are they not showing up in other cases. Question: When you guys have this errors, what are the steps to get rid of them and show the ads? (Of course I can take out the ads, but then my client would be unhappy, LOL)

EDIT: This is part of the code (confidential information was taken out from it and replaced with 'secret'). I hope that the question makes more sense now.

$GLOBALS['google']['client']='secret';
$GLOBALS['google']['https']=read_global('HTTPS');
$GLOBALS['google']['ip']=read_global('REMOTE_ADDR');
$GLOBALS['google']['markup']='xhtml';
$GLOBALS['google']['output']='xhtml';
$GLOBALS['google']['ref']=read_global('HTTP_REFERER');
$GLOBALS['google']['slotname']='secret';
$GLOBALS['google']['url']=read_global('HTTP_HOST') . read_global('REQUEST_URI');
$GLOBALS['google']['useragent']=read_global('HTTP_USER_AGENT');
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • 2
    When my question is down-voted I would like to read the reason of the down-vote, so I can rephrase my question to conform with the expectations. Down-votes can be helpful if paired with a comment telling me what is wrong with the question. If you think this is off-topic, then I can assure you that this is a programming question, as this is a bug caused by something yet unknown for me. I seek to get information to resolve the issue. Thanks. – Lajos Arpad Mar 04 '14 at 14:38
  • I don't understand which system are you using to load the ads... wouldn't it be easier to put them as an include()? –  Mar 04 '14 at 14:51
  • @SakhalTurkaystan, thanks for the question. I have two files, a header and a footer and they are included where they are needed to be included. I have not posted the exact error, as it gives information both about the project and about the adsense user of my client, that's confidential information, so I have included the nature of the error, but I get your point. I will edit the question to give you more information, but I do not know what system is used, as I am not a specialist of Google AdSense. – Lajos Arpad Mar 04 '14 at 15:01
  • I use a simple include() to place them in my pages. I use only the standard ones. I don't know the ones for mobiles. But fopen() is a function for loading files, and that is what I find odd. –  Mar 04 '14 at 15:03
  • fopen was part of the google code and we are not allowed to modify that, so I have to leave it at that even if it is strange. – Lajos Arpad Mar 04 '14 at 15:03
  • I see. I recognize the URL... my ads have the URL as –  Mar 04 '14 at 15:06
  • It is loaded through PHP and it has no show_ads.js in that, but I do not really know the inner mechanisms of google code, so you might be right. – Lajos Arpad Mar 04 '14 at 15:09
  • One thing, all the values sent in your GLOBALS array contains real data in all the cases? Do you log the values sent? Maybe checking what values you send when it gives you back the error you can have a clue about what is happening. – Federico J. Mar 08 '14 at 12:30
  • The whole code was sent by the client and I have no access to his AdSense account, therefore I cannot really check the validity of the globals. All I can guarantee is that the globals are taken and implemented precisely from a message sent by the client which states that they are correct. Difficult situation, as I cannot really ask him to give me his confidential data which is used to set up his AdSense account. All that I see is that the ads are sometimes showing up and sometimes not showing up, using the same code with the same globals. – Lajos Arpad Mar 08 '14 at 20:34
  • 1
    If the client is withholding info you need to find the problem, then I would say the main problem is the client himself. That said, one solution might be to use a different AdSense account for testing. – Nico Mar 09 '14 at 12:20
  • Second @Nico s statement. You need these information to debug the problem. Also with no error messages we can not find any course of the problem, neither can you. Use another account and post some error messages, please. – func0der Mar 10 '14 at 13:42
  • In my post you can find the error message of failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found. So it tries to reach something which does not exist. However, I do not know why does it try to reach that something. Is that due to the size of the ad not matching the expected size of the code? Or something else? My question was not asking for a solution. It was asking for a thought process for this kind of error with google ads which should lead to the solution. I know where the error manifests, but do not know the best practices to solve this kind of error. – Lajos Arpad Mar 10 '14 at 16:03
  • 1
    have you tried to use Curl ? `php curl` should work – Kodr.F Mar 12 '14 at 15:49
  • do you always get 404 from the fopen(...) request ? because you may use http://stackoverflow.com/questions/8018693/get-meaningful-information-when-fopen-fails-php-suphp And not show the header/footer if the fopen request is failing for some reason. – Gal Samuel Mar 12 '14 at 17:28
  • @GalSamuel not always, sometimes the ads are showing up, I do not know why they are showing up when they do and why not when they do not. – Lajos Arpad Mar 13 '14 at 13:17
  • Do you have any other errors on your logs around the time of each of those fopen errors? – PatomaS Mar 14 '14 at 02:37
  • Nope, these are the only errors and they happen occasionally. I do not see the reason, why? – Lajos Arpad Mar 16 '14 at 13:25

3 Answers3

1

Since the errors you mention are on a remote server, Id try to test that server as much as possible.

I'd try to discard a problem with my endpoint, which includes my ISP, my browser, firewall and proxy. After that, I'd try to test the other endpoint.

First, try to look for a pattern on the error logs, try to find if it happens because to many connection in a short time, requests from s specific location, old browser, specific version of browser, etc.

Too many connections on a short time period

This should not be a problem for Google servers, but it may indicate a strange situation and some requests may not be getting the normal or complete HTTP response. You can not really test for this, but you can try to detect it and react.

Old browser - Specific version of browser

May be a bug on the browser is sending an incomplete request, which by coincidence happens to be an invalid one and that points to a non-existent resource. May be the browser has a cache defect and is sending the 404 response without even checking the resource. You test this by using at least two different browsers and private session, that way you discard browser problems, caching and cookies. (This works only for JS).

Requests originated on a specific location

That location may have connection problems, or a defective ISP, or an overzealous caching service, or a problematic proxy. You can not test this, but you can try to detect it and react, for instance, not sending ads to those requests.

Firewall - proxy

Is your client's site name similar to a banned/blocked one? If so, and if by coincidence on a server has happened a typo, you may be affected each time a request passes though. Check on lists of banned sites.

Overload on your clients server

Is your client's site hosted on a shared hosting, or a hosting that may be overused? If so, strange behaviours may happen, for instance related to execution time limit of the command, or because it stablishes too many connections and some have to be dropped out, leaving some connection just hanging and waiting for answer. This is difficult to test, but you can check the server load, do a reverse IP check to see if there are many clients sharing the same IP, do cURL requests and check average time.

Time limit - redirections

There are different time limits on a whole connection process. You can check if there is a problem with this, at least from your system as user and from your client's server. Create a PHP file with a cURL session (fresh connection each time and reusing connection), execute it many times, and check the response time and redirections. Check if you always get the right response from the service, if it takes too long, if there are redirections, if there are redirections every time or just some times, test a different times.

EDIT (from Lajos Árpád)

Ad size mismatch

The code might be applicable for a given size, while you might use a different size. You should try to get information from your client about the issue.

Setting mismatch

This is a more general case for the above, but I described the problem of ad size independently, because ad size mismatch often occurs and it might be the real problem. Also, you should check whether the other important settings match, is there an issue with mishandling of something.

END OF EDIT

In my opinion, this should give you the answer you are looking for, and if it doesn't, you can provide more details on this question that can be useful for somebody else or use the results to ask/report on Google.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
PatomaS
  • 1,603
  • 18
  • 25
  • Your answer is excellent, almost perfect. I have added two other points, which were omitted from the original answer. The problem is not solved yet, but I have not asked for a solution, I have only asked for the optimal thinking process and best practices to solve this kind of issue, and your answer was pointing me to the right direction. Therefore I upvote and accept your answer and hope that the bounty of 100 points will reach your user. – Lajos Arpad Mar 16 '14 at 13:38
  • @LajosArpad: No, it never reached me, but still glad that you solved the problem, or at least you are in the way to do it. – PatomaS Mar 25 '14 at 07:51
  • Oh, shoot, I have sacrificed 100 points for this question and it did not reach you. – Lajos Arpad Mar 25 '14 at 08:31
0
fopen was part of the google code and we are not allowed to modify that, so I have to leave it at that even if it is strange

That's a double bind. Get rid of it. If it doesn't work, you can't get it to work.

I have found this nearly 7 (!) years old post complaining about your fopen call not working "on their end".

Urs
  • 4,984
  • 7
  • 54
  • 116
0

If some times fopen does works for you and some times it doesn't, you may use conditioned action in your code like this:

        <?php

            // Turn off all error reporting
            error_reporting(0);

          $url = 'http://pagead2.googlesyndication.com/pagead/ads';   //put your real url here
          $fp = fopen($url, 'r');

          // The variable $http_response_header will automagically be generated (array)
          /* you may explore the rsponse by un-commenting  the following
          foreach($http_response_header as $key=>$value){
              echo $key.'='.$value.'<br>';
          }
          */

          $response_code = @explode(' ', $http_response_header[0]);
            $response_code = (int)$response_code[1];

            if($return_code==200){
               // show the ad part
               // echo 'OK';
            } else {
                // whatever
                // echo 'OH.BOY';
            }

        ?>

Another way to go round this will be to use the javascript AdSense code instead. Your client may get you the javascript parts to include in the header/footer etc'. It usually looks like:

    <script type="text/javascript">
    <!-- google_ad_client = "pub-UNIQUE_NUMBER";
    /* 300x250, created dd/mm/yy */
    google_ad_slot = "11111111";
    google_ad_width = 300;
    google_ad_height = 250;
    //-->
    </script>
    <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>
Gal Samuel
  • 493
  • 2
  • 12
  • Javascript it not an option in this case, this code has to be on server-side. The problem occurs when I call the fopen function, not afterwards. I can hide the problem, but I want the ads to be shown without problems. – Lajos Arpad Mar 16 '14 at 13:27
  • if fopen doesn't return code 200 you can't show anything. use the PHP code above to check the code you get from the fopen call. – Gal Samuel Mar 17 '14 at 16:35