...

View Full Version : Help with URLConnection and PHP.



lschoen114
08-21-2009, 09:44 PM
So i am sort of lost at the moment on this. I posted another ad in the Java section and i got all of that straightened out, but i am having trouble with the PHP properly calling and running the commands.

What i am trying to do is load an external page(from a different server) that will show up on my page. The page is http://www.horsetrailerworld.com/remote/hitchinposttrailer/newinventory.asp .

What i've got so far is URLConnection.php the writes the connection code:


<?php
// CURL or fopen of URLs
class URLConnection {

/**
* The url
*/
protected $url;

/**
* The cache contents of the url
*/
protected $contents;

/**
* Default constructor
*/
public function __construct($url) {
$this->url = $url;
}

/**
* 'Getters' to return contents and url
*/
public function getContents(){
$this->readAndCacheContents();
return $this->contents;
}
public function getUrl(){
return $this->url;
}

/**
* Reads and cache the contents of the url
*/
public function readAndCacheContents() {
if ( !empty($this->contents) ) {
return;
}

if ( function_exists('curl_init') ) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Charset: UTF-8;')); // lets hope for a UTF-8 version
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'Cinnamon Statistics Engine/'. Cinnamon::getVersion());
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
$this->contents = curl_exec($ch);
curl_close($ch);
} else {
$fh = fopen($this->url, 'rb');
if ( $fh ) {
$this->contents = stream_get_contents($fh);
fclose($fh);
}
}

}
}
?>



Then i have remote.php that is supposed to give the URLConnection.php the page to connect to:


<?xml version="1.0" encoding="ISO-8859-1"?>
<?php
include 'URLConnection.php';

$obj = new URLConnection('http://www.horsetrailerworld.com/remote/hitchinposttrailer/newinventory.asp');
echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
echo $obj->getContents();
?>


After that i have test_load2.php that is supposed to show all the data:


<html>

<head>
<script language="javascript" type="text/javascript">


function loadXMLDoc(dname) {
var xmlDoc;
if (window.XMLHttpRequest) {
xmlDoc = new window.XMLHttpRequest();
xmlDoc.open("GET", dname, false);
xmlDoc.send("");
alert(xmlDoc.responseText);
return xmlDoc.responseXML;
}
// IE 5 and IE 6
else if (ActiveXObject("Microsoft.XMLDOM")) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.load(dname);
alert(xmlDoc.responseText);
return xmlDoc;
}
alert("Error loading document");
return null;
}

</script>

</head>

<body>


<script type="text/javascript">

xmlDoc = loadXMLDoc("remote.php");



</script>

</body>
</html>


Whats happening is that the alert in the test_load2.php is going off, but there is no message there. Also, the page is just coming up blank.

Does anyone out here know how to do this using what i have, or does anyone know another way of doing it? I want the page http://www.horsetrailerworld.com/remote/hitchinposttrailer/newinventory.asp to show up on my website similar to what #includes does but doing it from a seporate server location.

Thanks!

Fumigator
08-22-2009, 12:21 AM
Is there any compelling reason to use AJAX? You're complicating the process, so if it's not necessary, stop using it. You take the output from your curl method and echo it straight to the page which will accomplish what you want to do.

BTW I do hope you're not trying to steal other people's work (articles, etc) and pass it off as your own.

lschoen114
08-22-2009, 02:15 AM
I'm definatly not stealing anything. It is an inventory for a trailer company, that is hosted on a main dealer site similar to autotrader. I have permission to use the page.

I only used AJAX because that was the first thing that was recommended to me so i ran with it.

Do you mind me asking how i can echo it onto the page? Sorry, im not too great at PHP yet.

Thanks!

seco
08-22-2009, 02:35 AM
<?php echo file_get_contents('http://www.horsetrailerworld.com/remote/hitchinposttrailer/newinventory.asp'); ?>

you just want to show that page data on another site?

lschoen114
08-22-2009, 05:51 AM
Yeah, pretty simple. Just showing the information on that site to display on my site similar to #includes.

seco
08-22-2009, 05:55 AM
then just use the code i posted.

lschoen114
08-22-2009, 08:19 AM
It seems like its not working, maybe i am doing something wrong. Do i still need to call the URLConnection.php file, or do i just need to implement
<? echo file_get_contents($_GET['http://www.horsetrailerworld.com/remote/hitchinposttrailer/newinventory.asp']); ?> Into the page?

This is what i got, and it shows up blank.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<base href="http://www.horsetrailerworld.com/" />
</head>

<body>
<? echo file_get_contents($_GET['http://www.horsetrailerworld.com/remote/hitchinposttrailer/newinventory.asp']); ?>

</body>
</html>


Is that incorrect? I am still learning this so im sorry if i seem like i dont know what im talking doing. Im trying. :)

Thanks!

seco
08-22-2009, 04:37 PM
you changed the code i posted. just C&P what i posted, no need for GET.

lschoen114
08-22-2009, 11:25 PM
Still showing up blank. This is what im getting.

http://hitchinposttrailer.com/test_load3.php

I also tried it on a different server to see if maybe the server i was on didnt support it, and i got the same issue, its just white. I also tried a different file name and also still got a white page.

_Aerospace_Eng_
08-23-2009, 12:24 AM
Use EXACTLY the same code that seco gave you. Its likely your host has short open tags off so it won't work with the way you changed it.

seco
08-23-2009, 01:17 AM
yeah man, dont alter what i posted, just put it in your page.

lschoen114
08-23-2009, 01:22 AM
I've opened notepad, pasted

<?php echo file_get_contents('http://www.horsetrailerworld.com/remote/hitchinposttrailer/newinventory.asp'); ?>

and saved it and it came up blank.

I also tried

<?xml version="1.0" encoding="ISO-8859-1"?>
<?php echo file_get_contents('http://www.horsetrailerworld.com/remote/hitchinposttrailer/newinventory.asp'); ?>
comes up blank

Also tried

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php echo file_get_contents('http://www.horsetrailerworld.com/remote/hitchinposttrailer/newinventory.asp'); ?>
</body>
</html>

Still blank.

Am i doing something wrong? I also tried using a different link and it doesnt work. Could there be something wrong with the .asp page?

seco
08-23-2009, 01:28 AM
Must be your server settings, here is

<?php echo file_get_contents('http://www.horsetrailerworld.com/remote/hitchinposttrailer/newinventory.asp'); ?>

on my server

http://www.blueicestudios.com/trailer.php

lschoen114
08-23-2009, 01:45 AM
Thank you so much Seco!

I will get in touch with my hosting company right away to get it taken care of.

Really appreciate it.

seco
08-23-2009, 02:00 AM
Thank you so much Seco!

I will get in touch with my hosting company right away to get it taken care of.

Really appreciate it.

like aerospace said, check "short_open_tags"

_Aerospace_Eng_
08-23-2009, 05:11 AM
Also enabling error reporting might help.

<?php ini_set('display_errors','1');
error_reporting(E_ALL);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php echo file_get_contents('http://www.horsetrailerworld.com/remote/hitchinposttrailer/newinventory.asp'); ?>
</body>
</html>

lschoen114
08-24-2009, 10:28 PM
They said that the UNIX platform my hosting is on does not support .ASP. And since the file that i am trying to show is a .asp it is not working. So i have to upgrade to Windows platform i beleive.

Does that sound right to you guys?

lschoen114
08-24-2009, 10:33 PM
This is the error it gave me using the error reporting:

Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration in /hermes/bosweb/web017/b173/sl.hitchinp/public_html/test_load3.php on line 12

Warning: file_get_contents(http://www.horsetrailerworld.com/remote/hitchinposttrailer/newinventory.asp) [function.file-get-contents]: failed to open stream: no suitable wrapper could be found in /hermes/bosweb/web017/b173/sl.hitchinp/public_html/test_load3.php on line 12

seco
08-24-2009, 10:34 PM
my server is linux. file_get_contents has nothing to do with a file extention. its just the PHP version of a iframe. you want better hosting?

_Aerospace_Eng_
08-24-2009, 10:35 PM
They said that the UNIX platform my hosting is on does not support .ASP. And since the file that i am trying to show is a .asp it is not working. So i have to upgrade to Windows platform i beleive.
No they are wrong. file_get_contents doesn't parse the file as asp. It simply gets the contents it returns. Everything is parsed on the server that the file is on then the html is what you would get. My server doesn't support .asp files either but it shows the contents of that url just fine.

Okay so your host has set allow_url_fopen to off meaning it won't let you pull any content from any other server. They usually do this for security reasons. I don't think this is something you can set yourself so what you may have to do is use curl. Read this to understand what allow_url_fopen does.

http://wiki.dreamhost.com/Allow_url_fopen

You will need to do something like this

<?php
ini_set('display_errors','1');
error_reporting(E_ALL);
/**
* Get a web file (HTML, XHTML, XML, image, etc.) from a URL. Return an
* array containing the HTTP server response header fields and content.
*/
function get_web_page( $url )
{
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);

$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );

$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Trailers for Sale</title>
<base href="http://www.horsetrailerworld.com/remote/hitchinposttrailer/">
</head>

<body>

<?php
$result = get_web_page('http://www.horsetrailerworld.com/remote/hitchinposttrailer/newinventory.asp');

if ( $result['errno'] != 0 || $result['http_code'] != 200)
{
echo 'error: '.$result['errmsg'];
exit();
}
echo $result['content'];
?>
</body>
</html>

lschoen114
08-25-2009, 12:41 AM
This is what i got after doing that.

Notice: Use of undefined constant CURLOPT_AUTOREFERER - assumed 'CURLOPT_AUTOREFERER' in /hermes/bosweb/web017/b173/sl.hitchinp/public_html/test_load3.php on line 16

Fatal error: Call to undefined function: curl_setopt_array() in /hermes/bosweb/web017/b173/sl.hitchinp/public_html/test_load3.php on line 23


I also emailed my hosting provider to ask them if they can enable allow_url_fopen

_Aerospace_Eng_
08-25-2009, 09:46 PM
Hmm. Try this

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<title>Trailers for Sale</title>
<base href="http://www.horsetrailerworld.com/remote/hitchinposttrailer/">
</head>

<body>
<?php
/**
* Initialize the cURL session
*/
$ch = curl_init();
/**
* Set the URL of the page or file to download.
*/
curl_setopt($ch, CURLOPT_URL,'http://www.horsetrailerworld.com/remote/hitchinposttrailer/newinventory.asp');
/**
* Ask cURL to return the contents in a variable
* instead of simply echoing them to the browser.
*/
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
/**
* Execute the cURL session
*/
$contents = curl_exec ($ch);
echo $contents;
/**
* Close cURL session
*/
curl_close ($ch);
?>
</body>
</html>

lschoen114
08-27-2009, 07:22 AM
Works great! Thank you!

_Aerospace_Eng_
08-27-2009, 03:46 PM
I think you should switch hosts. They are still using PHP4. PHP5 has been out for a while already.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum