cancel
Showing results for 
Search instead for 
Did you mean: 

requesting pages from ccgi by scripts

mharrison
Newbie
Posts: 2
Registered: ‎22-05-2011

requesting pages from ccgi by scripts

Hi,
I am trying to access a php file on my ccgi space using a script on another server (remote to plusnet).
Instead of getting the scripts output the ccgi server returns a 404 error and also states that it had a problem finding the error page.
if i access the file directly from a web browser it works fine.
Any help appreciated.
The code I am using is

<?php
$server = "ccgi.xxxx.plus.com";
$port = "80";
$content = "";
$fp = fsockopen("$server", $port, $errno, $errstr, 1);
if($fp) {
          fputs($fp, "GET /myfile.php HTTP/1.0\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)\r\n\r\n");
          while (!feof($fp)) {
            $content .= fgets($fp,128);
          }
          fclose($fp);
          if(!strcmp("OK",substr($content,0,2))) {
            echo "OK";
          }
          else {
            echo "ERROR:contents = $content";
          }
    }
    else {
      echo "ERROR:";
      echo $errno + "-" + $errstr;
    }
?>


3 REPLIES 3
spraxyt
Superuser
Superuser
Posts: 10,063
Thanks: 1,369
Fixes: 75
Registered: ‎06-04-2007

Re: requesting pages from ccgi by scripts

I think the problem might be that since ccgi is a shared server the GET request has to provide host information so the server knows which virtual host is to supply the requested content. Though this information was given in the fsockopen call this will have provided only a handle to the shared IP of the server.
Try including an additional header
Host: ccgi.xxxx.plus.com:80\r\n
following the GET request.
Additionally I'm not sure that fputs can be relied upon to transmit the complete string to the server (it might terminate early). See the first note in the PHP manual fwrite function document.
I think the first characters of the response will be HTTP/1.0 200 OK (on success) so the check for success would be to look for the 200 code at location 9 (the text OK is normal but not guaranteed).
David
mharrison
Newbie
Posts: 2
Registered: ‎22-05-2011

Re: requesting pages from ccgi by scripts

It looks ilke that was the problem. I had suspected it may have been the virtual hosting nature of the server in some way.
I found that the port should be left off the Host header otherwise it still fails but after a longer pause!
I will look into the fputs issue you mention.
You are also right about the responses. The code snippet was slightly edited and also not finished - I haven't quite decided what I need the script to return yet....
Thanks for the help  Smiley
Ben_Brown
Grafter
Posts: 2,839
Registered: ‎13-06-2007

Re: requesting pages from ccgi by scripts

Depending on the server configuration you could simplify that php significantly with file_get_contents.