The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 22, 2019, 08:26:18 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: grab content from another site  (Read 5799 times)
tomt
Rookie
**
Offline Offline

Posts: 18


View Profile
« on: October 26, 2009, 11:44:35 AM »

I am need to know how to grab content from another site (my own) and display it on a php page. I know how to do this in coldfusion

<cfhttp url = "http://mysite.com" method = "get" resolveURL = "yes">
<cfoutput>#cfhttp.fileContent#</cfoutput>

Very simple. This code grabs the entire source code of the url and displays it as part of the page. I am pretty sure the same thing can be done in php but don't know how.

Thanks
Logged

No links in signatures please
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: October 26, 2009, 12:07:17 PM »

<?php

echo file_get_contents("http://www.mysite.com{$_GET['REQUEST_URI']}");

?>

This will append the original requested URI to the call from the remote site - note that this will be vital in the case of passing through graphics and such.

I have not checked this to make sure it's OK - this is straight from my @$$.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
tomt
Rookie
**
Offline Offline

Posts: 18


View Profile
« Reply #2 on: October 26, 2009, 03:41:21 PM »

Images did not come through. In coldfusion that is what the resolve url part does. It appends the domain name to links that do not have them.

Thanks
Logged

No links in signatures please
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #3 on: October 26, 2009, 04:01:08 PM »

Oh, wait I'm an idiot - I think about everything from my perspective of a mod-rewrite site into a single URL.

This will work, and graphics will float through if you mod_rewrite the site so that every request for EVERYTHING comes into this PHP Script.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
tomt
Rookie
**
Offline Offline

Posts: 18


View Profile
« Reply #4 on: October 26, 2009, 04:43:33 PM »

I just figured out I don't need the pictures any way. My source page can also be generated by javascript code. So all I need is to be able import the javascript. The javascript then does the rest, bringing in images and text. So what you have there works perfectly for me.

Thanks
Logged

No links in signatures please
tomt
Rookie
**
Offline Offline

Posts: 18


View Profile
« Reply #5 on: October 26, 2009, 04:49:18 PM »

I didn't see your reply before I posted my last message. Here is what I am doing. I have several wpmu sites. I am creating a centralized ad posting based on cgi referrer code. Of course I am writing this in cf because that is what I know best. I just needed the display code for my wordpress template. And as said your solution works.

Thanks,
Logged

No links in signatures please
tomt
Rookie
**
Offline Offline

Posts: 18


View Profile
« Reply #6 on: October 26, 2009, 05:54:18 PM »

I just improved this to be even better. Doing a google search I found this function at http://www.namepros.com/programming/53456-parse-a-url-php-return-domain.html.

<?php

$myurl = "http: //www . thesite . com";

function parse_url_domain ($url) {
$parsed = parse_url($url);
$hostname = $parsed['host'];
return $hostname;
}

$raw_url = parse_url($myurl);
$domain_only =str_replace ('www.','', $raw_url);
$theurl = "http: //www. mysite.com/s/a.cfm?aver=" . $domain_only['host'];

echo file_get_contents($theurl);

?>

I can get $myurl to equal the domain of the site using a wordpress function. And on the coldfusion side all I have to do is run the url variable aver through a switch case statement to get the correct ad for that site. Or could place the ads in a database and do a simple sql request. Either way this is going to make updating ads very, very easy.


<edit: URL munged by Perk>
« Last Edit: October 26, 2009, 05:56:33 PM by perkiset » Logged

No links in signatures please
tomt
Rookie
**
Offline Offline

Posts: 18


View Profile
« Reply #7 on: November 04, 2009, 07:17:42 AM »

I am using Perkeset's solution for now. For my last solution to work I have to first assign categories to each blog and I haven't had the time to do that yet. Any way I discovered this morning the server that my ad server is on was down. This resulted in this being displayed across the top of each blog

Warning: file_get_contents() [function.file-get-contents]: php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution

Again php is not my strong suit. In coldfusion I would place a cftry/cfcatch statement around it. Is there some kind of if statement that I can place around my code to catch this error that would just ignore the get content command and just process the rest of the page?

Thanks
Logged

No links in signatures please
ehlo
Journeyman
***
Offline Offline

Posts: 50


View Profile
« Reply #8 on: November 04, 2009, 07:38:18 AM »

You can use a similar approach in php:

Code:
<?php
try
{
  
// code to run here
}
catch(
Exception $e)
{
  
// handle exception here
}
?>


However in my sites I always turn off error reporting in the php.ini or by including

Code:
<?php ini_set('display_errors'0); ?>

at the top of the page. This will work as long as the error is not fatal in which case the page won't display.
« Last Edit: November 04, 2009, 07:41:01 AM by ehlo » Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #9 on: November 04, 2009, 08:59:45 AM »

There's also

error_reporting(0);

which turns off all reporting, but does it for the local instance only (my preference).
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
tomt
Rookie
**
Offline Offline

Posts: 18


View Profile
« Reply #10 on: November 04, 2009, 09:36:18 AM »

I used <?php ini_set('display_errors', 0); ?> as I could see what it meant and how to use it. Problem solved. Thanks
Logged

No links in signatures please
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!