Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    New Coder
    Join Date
    Aug 2009
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Help with URLConnection and PHP.

    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/rem...winventory.asp .

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

    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:

    Code:
    <?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:

    Code:
    <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/rem...winventory.asp to show up on my website similar to what #includes does but doing it from a seporate server location.

    Thanks!

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.

  • #3
    New Coder
    Join Date
    Aug 2009
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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!

  • #4
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts
    PHP Code:
    <?php echo file_get_contents('http://www.horsetrailerworld.com/remote/hitchinposttrailer/newinventory.asp'); ?>
    you just want to show that page data on another site?

  • #5
    New Coder
    Join Date
    Aug 2009
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Yeah, pretty simple. Just showing the information on that site to display on my site similar to #includes.

  • #6
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts
    then just use the code i posted.

  • #7
    New Coder
    Join Date
    Aug 2009
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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
    Code:
    <? 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.
    Code:
    <!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!

  • #8
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts
    you changed the code i posted. just C&P what i posted, no need for GET.

  • #9
    New Coder
    Join Date
    Aug 2009
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.

  • #10
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    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.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #11
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts
    yeah man, dont alter what i posted, just put it in your page.

  • #12
    New Coder
    Join Date
    Aug 2009
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I've opened notepad, pasted
    Code:
    <?php echo file_get_contents('http://www.horsetrailerworld.com/remote/hitchinposttrailer/newinventory.asp'); ?>
    and saved it and it came up blank.

    I also tried
    Code:
    <?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
    Code:
    <!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?

  • #13
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts
    Must be your server settings, here is
    PHP Code:
    <?php echo file_get_contents('http://www.horsetrailerworld.com/remote/hitchinposttrailer/newinventory.asp'); ?>
    on my server

    http://www.blueicestudios.com/trailer.php
    Last edited by seco; 08-23-2009 at 02:31 AM.

  • Users who have thanked seco for this post:

    lschoen114 (08-23-2009)

  • #14
    New Coder
    Join Date
    Aug 2009
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.

  • #15
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts
    Quote Originally Posted by lschoen114 View Post
    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"


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •