user503076
user503076

Reputation:

Call webpage from another server into your website using php

I am sorry if this is a duplicate.

I am creating an application in PHP and Codeigniter for my client and he has a strange request. There is a link on the website which shows diamond report from IGI website. The IGI website is made in ASP.net and uses query strings to show a report. My application opens that report in a new pop up window. Since, it is another server and uses query string the url is shown in web page source.

Now, he wants to camouflage the url or does not want anybody to see the external IGI report url in the source of the webpage. How can I implement this functionality? I told him that this is not possible as the IGI server itself uses query strings.

Is this possible? Here is the url to the report:

http://www.igiworldwide.com/search_report.aspx?PrintNo=S3B30818&Wght=0.13

Now he does not want that the above url be shown in the source but want it something like http://www.hiswebsite.com/certificate/1234567879 which shows the report from IGI website.

I am puzzled.

Gaurav

Upvotes: 0

Views: 3818

Answers (3)

SuperSpy
SuperSpy

Reputation: 1314

EDIT2: Demo of my solution: http://pwslogboek.nl/screen-scraping-example

In most cases this will work:

$source = file_get_contents(LINK);

This is an alternative:

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, LINK_OF_OTHER_WEBSITE); // The link of the site
curl_setopt($curl, CURLOPT_ENCODING, 'gzip');           // Makes the request faster
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);       // Return the source as string
$source = curl_exec($curl);

Now you can do anything you like with the source.

Clean it from hrefs:

    $cleanSource= preg_replace('|href=\'(.*)\'|', '', $source);

If you need to post something you'll need the extra curl options:

$postFields = array(
    'user'     => 'username',
    'password' => 'password'   );
$postData = http_build_query($postFields);

curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);

I personally dislike DOMDocuments... For simple scripts this will do fine.

This is the script I use to retrieve in my script a table: (EDIT: This function will now get you the whole table)

$table = getTagWithContents($source, 'table', '<table tabindex="13" class="tableBg"');

// **********************************************************************************
// Gets a whole html tag with its contents.
//  - Source should be a well formatted html string (get it with file_get_contents or cURL)
//  - You CAN provide a custom startTag with in it e.g. an id or something else (<table style='border:0;')
//    This is recommended if it is not the only p/table/h2/etc. tag in the script.
//  - Ignores closing tags if there is an opening tag of the same sort you provided. Got it?
function getTagWithContents($source, $tag, $customStartTag = false)
{

    $startTag = '<'.$tag;
    $endTag   = '</'.$tag.'>';

    $startTagLength = strlen($startTag);
    $endTagLength   = strlen($endTag);

//      ***************************** 
    if ($customStartTag)
        $gotStartTag = strpos($source, $customStartTag);
    else
        $gotStartTag = strpos($source, $startTag);

    // Can't find it?
    if (!$gotStartTag)
        return false;       
    else
    {

//      ***************************** 

        // This is the hard part: finding the correct closing tag position.
        // <table class="schedule">
        //     <table>
        //     </table> <-- Not this one
        // </table> <-- But this one

        $foundIt          = false;
        $locationInScript = $gotStartTag;
        $startPosition    = $gotStartTag;

        // Checks if there is an opening tag before the start tag.
        while ($foundIt == false)
        {
            $gotAnotherStart = strpos($source, $startTag, $locationInScript + $startTagLength);
            $endPosition        = strpos($source, $endTag,   $locationInScript + $endTagLength);

            // If it can find another opening tag before the closing tag, skip that closing tag.
            if ($gotAnotherStart && $gotAnotherStart < $endPosition)
            {               
                $locationInScript = $endPosition;
            }
            else
            {
                $foundIt  = true;
                $endPosition = $endPosition + $endTagLength;
            }
        }

//      ***************************** 

        // cut the piece from its source and return it.
        return substr($source, $startPosition, ($endPosition - $startPosition));

    } 
}

Upvotes: 0

giorgio
giorgio

Reputation: 10212

It is indeed quite odd :)

If you're in to some fiddling you could use fopen to open the page, use some DOM inspection to retrieve the table you'd want, and then display only that table on your own website.

$page = file_get_contents('http://www.somepage.com/');
$dom = new DOMDocument();
$doc = $dom->loadHTML($page);
$tables = $doc->getElementsByTagName('table');
// find out which table you need and do something with it

Upvotes: 3

Eineki
Eineki

Reputation: 14959

There are several way to do this:

You can use curl to fetch the page with php then you can serve the result without exposing the call to igiworldwide.

If you have an http wrapper enabled you can just open the file with a call similar to

readfile('http://www.igiworldwide.com/search_report.aspx?PrintNo=S3B30818&Wght=0.13');

And Yes, there are a lot of duplicates of this question on stackoverflow

Upvotes: 0

Related Questions