Import PHP from one page into another?

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 3287 Views - Last Post: 19 August 2014 - 09:36 AM Rate Topic: -----

#1 ablac   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 14-August 14

Import PHP from one page into another?

Posted 14 August 2014 - 09:05 PM

I've been browsing the internet for 3 days trying to figure this out have have tried everything i could from from Iframe imports (Which look really really bad), to Ajax Imports (Which i cant quite get 100% working), Nothing has worked to my standards :(

The setup: I have 2 websites running an Xenforo Forum, and a Wordpress Frontend. I'm trying to essentially merge the 2 together without getting an expensive bridge software for Xenforo (49.99...). The only part im worried about is the Header/Navigation within Xenforo, this includes the Logo Block, Login/Signout, Forum navigation, as well as User Navigation(Messages, Alerts, ECT).

I'm trying to move this into a custom Wordpress Theme, I already have the location within the theme that it would need to be placed in order to fit properly.

The header section of the Xenforo page is stored within a Div called "header", I've been looking into Div importing.

If you need any further information let me know :)

Thanks :)

Is This A Good Question/Topic? 0
  • +

Replies To: Import PHP from one page into another?

#2 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2768
  • View blog
  • Posts: 8,077
  • Joined: 15-January 14

Re: Import PHP from one page into another?

Posted 15 August 2014 - 09:22 AM

Quote

I've been browsing the internet for 3 days trying to figure this out...
I'm trying to essentially merge the 2 together without getting an expensive bridge software for Xenforo (49.99...).

At some point you have to ask yourself how much your time is worth. Is spending 50 bucks worth 3 days?

The only concern I have is that the content you are trying to import clearly depends on the user's login session. If you went with something like CURL, where you download the contents of the page on the server, parse the HTML, extract what you're looking for, and add it to the other content, then you'll also need to make sure that CURL is passing all of the user's cookies through in the request. If Xenforo uses dynamic CSS or Javascript then that's another issue, you would need to make sure you also get that part of the page and add that also (and hopefully the Javascript isn't trying to do anything with any elements other than what is in the header).

Ajax would work also and would have a similar process, you would need to get the contents of the page, parse everything, and extract the content you're looking for. You could also use a combination where you use ajax to get the HTML, send that to PHP, and have PHP parse it and return the relevant section for Javascript to add to the page.
Was This Post Helpful? 2
  • +
  • -

#3 ablac   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 14-August 14

Re: Import PHP from one page into another?

Posted 15 August 2014 - 09:30 AM

I don't know if this makes it any easier but both sites are on the same server under the same domain, and even share the same MYSQL database, though the database is on a separate server.I was able to pull the Header DIV but it would show up in the browser saying you need a plug in to view this content. I'll post the code when I get back to my house, maybe I did something wrong.

Also I am learning how to use Ajax so if you would be able to possibly send me some code and explain how it works so I can learn it that would be awesome :) I can also post the Xenforo Navigation template, if that will help.
Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2768
  • View blog
  • Posts: 8,077
  • Joined: 15-January 14

Re: Import PHP from one page into another?

Posted 15 August 2014 - 09:35 AM

You need more than just code to understand ajax, you need to understand what it is actually doing. Ajax is how you use Javascript to have the browser send a request, like any other request. When you click a link your browser sends a request for an HTML page (or whatever the URL points to). When it's loading the page and sees things like CSS stylesheets or Javascript files or images, it sends another request to get each of those files. The web server responds with a response to the request, and the browser handles the response from the server. Ajax is a way for you to do that process manually. You tell the browser where to send the request, you set up code to handle the response when it comes back, and when the response comes back your code runs and handles the response. The request goes out in the background, so the user doesn't see the browser sitting there loading something, it just runs in the background. Start here, there are eventually some code examples about setting up the request object and handling the response.

http://www.w3schools...jax/default.ASP

In your case ajax will be an option since you are sending the request to the same domain. With default security settings, browsers will not send ajax requests to a different domain than the one where the page originated.

At the end of the day though, you're responsible for writing the code to set this up on your site. We're happy to answer your questions, but we won't just do your work for you. If you want to learn then we can teach you.

This post has been edited by ArtificialSoldier: 15 August 2014 - 09:36 AM

Was This Post Helpful? 0
  • +
  • -

#5 ablac   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 14-August 14

Re: Import PHP from one page into another?

Posted 15 August 2014 - 11:19 AM

I read through the Ajax tutorial, and am still confused, but did try and create a few codes, but have also realized, after further investigation, and index.php for Xenforo is just a Compiler... so if i want to pull the Navigation Div i need to find the location of that div to get it to work...

but heres the 2 codes I made up.

<div id="siteloader"></div>​
    <script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
    <script>
      function LoadPage(){
		$.get('/index.php #navigation', function(data) {
		$('#siteloader').html(data);
		onload="LoadPage()"
  });
}
    </script>


	<ol id="XF-Import"></ol>
	 <script src="//code.jquery.com/jquery-1.10.2.js"></script>
	<script>
		$( "#XF-Import" ).load( "/index.php #navigation li" );
	</script>

Was This Post Helpful? 0
  • +
  • -

#6 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2768
  • View blog
  • Posts: 8,077
  • Joined: 15-January 14

Re: Import PHP from one page into another?

Posted 15 August 2014 - 11:25 AM

I don't use jQuery, so I'm not sure if trying to load "index.php #navigation" is going to cause jQuery to download index.php and then use a selector to find an element with the ID "navigation". I don't think it is, but like I said I'm not familiar with how jQuery does that.

In the first piece of code, are you executing that function? Adding the line onload="LoadPage()" inside the ajax response handler is not going to execute the LoadPage function.
Was This Post Helpful? 0
  • +
  • -

#7 ablac   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 14-August 14

Re: Import PHP from one page into another?

Posted 15 August 2014 - 11:43 AM

I don't know :( I tried moving the
onload="LoadPage()"
into the [Body] but it still didn't work :(

I also tried something else, also got nothing.
<embed id="divID" src="/index.php/#header" width='100%' height='175px'/>
<div id="divID" style="display:block !important;"></div>

Was This Post Helpful? 0
  • +
  • -

#8 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2768
  • View blog
  • Posts: 8,077
  • Joined: 15-January 14

Re: Import PHP from one page into another?

Posted 15 August 2014 - 11:47 AM

You're just kind of trashing around at this point. This:

src="/index.php/#header"

Is going to load the URL "/index.php/#header", it is not going to load index.php and pull in only the element with the ID "header". Stop thrashing around and focus, instead of blindly trying things that aren't going to work you need to understand what you're doing and make something that is going to work.

If you want to call a Javascript function, just add the line to call the function after you define the function. You don't even need a function in that case, just put the statements outside of a function and the browser will execute that code when it gets to it. If you want to use ajax to load the page, there is plenty of information about how to do that with jQuery.

https://www.google.c...=UTF-8&oe=UTF-8
Was This Post Helpful? 0
  • +
  • -

#9 ablac   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 14-August 14

Re: Import PHP from one page into another?

Posted 15 August 2014 - 12:15 PM

Sorry, I'm just kind of frustrated :( I honestly didn't think it would be this challenging -_-

So to make my life easier, I setup a new page on my server called /dev/index.html, to hopefully get the code to work before putting it into the wordpress header -_- (Cause I'm crazy and was actually editing it directly trying to figure this out...)

After looking through some of the tutorials you linked to I found/modified this, and its still not working lol... Which brings up a slight question, when I Inspect the Page elements with Firefox. My site uses Cloudflare as a CDN and JS CSS & HTML optimizations, do you think this would have any effect on this type of code working?

Heres the code i created, I get the Hello, but nothing else.

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script>
    $(document).ready(function(){
        $.post("index.php",{},function(response){
            $(response).find('.header').each(function(){
                $('#main').append($(this).html());
            });
        });
    });
</script>
</head>
<body>
    <div id="main">
    Hello...
    </div>
</body>
</html>

Was This Post Helpful? 0
  • +
  • -

#10 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2768
  • View blog
  • Posts: 8,077
  • Joined: 15-January 14

Re: Import PHP from one page into another?

Posted 15 August 2014 - 01:42 PM

Quote

My site uses Cloudflare as a CDN and JS CSS & HTML optimizations, do you think this would have any effect on this type of code working?

That's hard to say without seeing everything.

I would start by adding some debugging statements to that code to help you figure out what is going on, e.g.:

    $(document).ready(function(){
        console.log('sending request to index.php');
        $.post("index.php",{},function(response){
            
            console.log('ajax response:');
            console.log(response);

            console.log('header:');
            console.log($(response).find('.header'));

            $(response).find('.header').each(function(){
                console.log('appending html:');
                console.log($(this).html());
                $('#main').append($(this).html());
            });
        });
    });


You're sending a post request, which might be fine but the forum might be set up to handle post requests differently if it assumes that a form was submitted. Normal requests are get requests, not post requests. Otherwise, check your developer console to see what it shows when the response comes back. You are getting the response from the server and finding elements with the class "header".
Was This Post Helpful? 0
  • +
  • -

#11 ablac   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 14-August 14

Re: Import PHP from one page into another?

Posted 15 August 2014 - 01:50 PM

xenforo.com said:

XenForo does not leave its HTML code in the file system like you have been used to for years in Wordpress. You must access the compiled version of the template from XenForo's database, and pass it through XenForo's template renderers to get usable HTML. There is not an easy way to do this, because everything really is tied to index.php and all the outputs of various classes are designed to cascade into index.php in a particular way.

That said, it is completely possible to do what you are asking. I have personally done something very similar in the past for custom site pages until XenForo 1.2 added a feature that let me route the requests through index.php and made it ... somewhat easier. That is not something you can really do with Wordpress.

If you don't want to go through all the time of reverse engineering a bunch of XenForo to figure out how to get the output of 1 template into Wordpress, you might just want to copy and paste the content of the header template into a Wordpress node and then just use that. It won't be linked and won't be updated automatically if XenForo's header is, but it will be easier.

I'm sure you already know you can look at the contents of various template's in XenForo's admin area, under Appearance > Templates.


I don't think it will be possible to pull the Header with a Ajax/Jquery call :(

Copying the Template over doesnt work though cause Xen has xen(Code) which wordpress/normal HTML/php doesnt read.

<xen:edithint template="header.css" />

<xen:hook name="header">
<div id="header">
	<xen:include template="logo_block" />
	<xen:include template="navigation" />
	<xen:if is="{$canSearch}"><xen:include template="search_bar" /></xen:if>
</div>
</xen:hook>



Was This Post Helpful? 0
  • +
  • -

#12 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2768
  • View blog
  • Posts: 8,077
  • Joined: 15-January 14

Re: Import PHP from one page into another?

Posted 15 August 2014 - 01:54 PM

Quote

I don't think it will be possible to pull the Header with a Ajax/Jquery call

What makes you think that?

Quote

Copying the Template over doesnt work though cause Xen has xen(Code) which wordpress/normal HTML/php doesnt read.

It's also impractical, because you need the forum to execute all of the code it would otherwise normally execute to deal with the user's login session and whatever else. You need the rendered processed HTML.
Was This Post Helpful? 0
  • +
  • -

#13 ablac   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 14-August 14

Re: Import PHP from one page into another?

Posted 15 August 2014 - 02:15 PM

I ran the code with the Console responses, and this showed up in the Debugger...

http://pastie.org/9476508

Sadly that giant blob of Code is only from Cloudflare, nothing is from the Index.php file.

I had a though about that though, currently the test file is in /dev/index.html
and the file were trying to access is in /index.php.

When calling for index.php should i put the full path /var/www/html/index.php?

I don't know if this will help at all, but this is what index.php looks like, in an editor.
[code='Index.php']
<?php

$startTime = microtime(true);
$fileDir = dirname(__FILE__);

require($fileDir . '/library/XenForo/Autoloader.php');
XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library');

XenForo_Application::initialize($fileDir . '/library', $fileDir);
XenForo_Application::set('page_start_time', $startTime);

$fc = new XenForo_FrontController(new XenForo_Dependencies_Public());
$fc->run();
[code]
Was This Post Helpful? 0
  • +
  • -

#14 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2768
  • View blog
  • Posts: 8,077
  • Joined: 15-January 14

Re: Import PHP from one page into another?

Posted 15 August 2014 - 02:53 PM

Quote

When calling for index.php should i put the full path /var/www/html/index.php?

Not the filesystem path, the HTTP path. You're sending an HTTP request, not opening a file on disk. Javascript is running in the client's browser, it can't directly access files on the server's hard drive.
Was This Post Helpful? 0
  • +
  • -

#15 ablac   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 14-August 14

Re: Import PHP from one page into another?

Posted 15 August 2014 - 06:06 PM

I'm getting a new error now.

Use of getUserData() or setUserData() is deprecated.  Use WeakMap or element.dataset instead. requestNotifier.js:63
ReferenceError: $ is not defined dev:10


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2