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
About

hi there, I am software engineer, working in e-commerce company and passionate about all thing digital. On this blog, I share my experiments with different technology.

Tagged with: ,
Posted in html, JavaScript, PHP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What people say about post?
seo hizmeti on Awesome wordpress ecommerce…

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 38 other followers

%d bloggers like this: