how to make web call to other website using php

There are many way to get around with cross site scripting.
When you want to make call to other website or other domain, there is limitation of javascript which does not allow to cross-site scripting.
When you try to make call you usually end up getting following error message.

“not allowed by Access-Control-Allow-Origin”

However, there is a solution to this problem, you can create request in php(.net use webrequest, java equivalent to webclient) using fopen to any URL and get content from outside your domain and parse the output you want.

following the following source code to how you can use php to make request to other website without any error.

function getcontent($url)
{
    $cont = "";
    $fp = fopen($url,"r");
    if (!$fp)
    {
        return "Unknown";
    }
    else
    {
        $com = "GET $file HTTP/1.1\r\nAccept: */*\r\nAccept-Language: de-ch\r\nAccept-Encoding: gzip, deflate\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)\r\nHost: $server:$port\r\nConnection: Keep-Alive\r\n\r\n";
        fputs($fp, $com);
        while (!feof($fp))
        {
            $cont .= fread($fp, 2000);
        }
        fclose($fp);
        $cont = substr($cont, strpos($cont, "\r\n\r\n") + 4);
        return $cont;
    }

$content = getcontent("http://www.google.com.au");
echo $content;
Advertisements