Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryWeb Page and Site Features

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Using JavaScript To Publish PHP Software Output

Some PHP software has desired content as its output. Examples are quote of the day, up-to-the-minute pricing, real time database information, and the site user's IP address.

Some publishing platforms, WordPress for example, don't allow PHP code in blog posts or article page content. Thus, the output of PHP software can not be published that way.

NOTE: The Insert PHP WordPress plugin is now available to insert PHP code directly into posts and pages.

But JavaScript can be used in most cases, even when PHP can not be used directly. JavaScript can import content from PHP software so long as the content is in the form of JavaScript code. And that's the key, it has to be JavaScript code.

How It Works

JavaScript can't publish the PHP software's output because generally the PHP output is not JavaScript code. A script tag on the web page with src URL of the desired PHP software won't work.

What can be done is use the PHP gateway software provided with this article. In the script tag, specify the URL of PHP gateway software.

The gateway software grabs the output of the desired PHP software, converts it to JavaScript, and makes it available to the web page.

The PHP To JavaScript Gateway Software

Copy the PHP code below and save it as file gateway.php (or other file name that works for you so long as it ends with ".php")

<?php
/* 
   PHP To JavaScript Gateway
   Version 1.0
   April 9, 2012

   Will Bontrager Software, LLC
   https://www.willmaster.com/
   Copyright 2012 Will Bontrager Software, LLC

   This software is provided "AS IS," without 
   any warranty of any kind, without even any 
   implied warranty such as merchantability 
   or fitness for a particular purpose.
   Will Bontrager Software, LLC grants 
   you a royalty free license to use or 
   modify this software provided this 
   notice appears on all copies. 
*/

if( isset($_GET['url']) )
{
   $contentlines = preg_split( '/[\r\n]+/', GetHTTPpage($_GET['url']) );
   foreach($contentlines as $s) { PrintLineAsJavaScript($s); }
}
else { PrintLineAsJavaScript('Please provide a URL'); }

function GetHTTPpage($url)
{
   $version = '1.0';
   $head = $content = $host = $uri = '';
   $url = preg_replace('/^https?:\/\//i','',$url);
   @list($host,$uri) = explode('/',$url,2);
   $domain = strtolower($host);
   $uri = "/$uri";
   $headsend = "GET $uri HTTP/$version\r\nHost: $host\r\nAccept: */*\r\n";
   if( isset($_SERVER['HTTP_USER_AGENT']) ) { $headsend .= 'User-Agent: '.$_SERVER['HTTP_USER_AGENT']."\r\n"; }
   $headsend .= "Connection: Close\r\n\r\n";
   $fp = @fsockopen($host,80,$errno,$errstr,20);
   if( ! $fp ) { return "Error number: $errno\nError string: $errstr\nDomain: $host\nURI: $uri"; }
   fwrite($fp,$headsend);
   while (!feof($fp)) { $content .= fgets($fp,1024); }
   fclose($fp);
   return substr( $content, strpos($content,"\r\n\r\n") );
} # function GetHTTPpage()

function PrintLineAsJavaScript($s)
{
   $s = str_replace("\\","\\"."\\",$s);
   $s = str_replace("'","\\"."'",$s);
   $s = str_replace("<!--","<'+'!--",$s);
   $s = str_replace("-->","--'+'>",$s);
   $s = preg_replace('/(scr)(ipt)/i',"$1'+'$2",$s);
   $s = preg_replace('/(win)(dow)/i',"$1'+'$2",$s);
   $s = preg_replace('/(doc)(ument)/i',"$1'+'$2",$s);
   echo "document.writeln('$s');\n";
} # function PrintLineAsJavaScript()
?>

No customization is required. Simply upload gateway.php to your server and make a note of its URL.

Using the Gateway Software

Now that the gateway software is installed, it can be used.

First, determine the URL of the PHP software with the content you desire. The PHP software can be anywhere on the Internet.

(Note: If the PHP software is on a domain you do not control, the domain may have implemented restrictions that prevent grabbing the page or parts of the page. In addition, if the grabbed content contains relative links or URLs, they will be broken.)

To proceed, you now have both the URL of the gateway software and the URL of the PHP software with the desired content. Construct a script tag with this format:

<script 
   type="text/javascript" 
   src="GATEWAY_URL?url=CONTENT_URL">
</script>

Note to WordPress users: For posts and pages, the script tag must be all one line. Like:

<script type="text/javascript" src="GATEWAY_URL?url=CONTENT_URL"></script>

Let's assume the gateway URL is http://www.example.com/gateway.php and the PHP software with the desired content is http://content.example.com/desired.php. Using the above format, the script tag becomes:

<script 
   type="text/javascript" 
   src="https://www.example.com/gateway.php?url=http://content.example.com/desired.php">
</script>

When the PHP software gets the import request, it grabs the page from the Internet, converts the content to JavaScript, and replies with the converted content. The content is then published on the web page.

What PHP Software Can Be Used

If the PHP software outputs content when it is loaded directly into the browser, it can be used with this system.

Otherwise, it shouldn't be used. Perhaps the PHP software relies on other PHP software to operate. Or perhaps it just doesn't publish any content.

A Special Case

If the URL of the PHP software with the desired content contains a query string parameter (a "?" followed with name=value data), then the PHP software URL needs to be encoded before use. The reason is that the gateway URL already contains a "?" followed by data and only one "?" is allowed.

To encode a URL, this JavaScript may be used.

<script type="text/javascript">
var URL = "http://content.example.com/desired.php?a=b&c=d";
document.write( escape(URL) );
</script>

Put the above JavaScript on a web page. Specify the URL to be encoded the first line below the script tag. Load the page into a browser. The script will print the encoded URL on the page.

If the URL of the PHP software with the desired content is http://content.example.com/desired.php?a=b&c=d, then the URL encoded with the above JavaScript becomes http%3A//content.example.com/desired.php%3Fa%3Db%26c%3Dd

The script tag to publish content from the desired PHP software using gateway.php then is:

<script 
   type="text/javascript" 
   src="https://www.example.com/gateway.php?url=http%3A//content.example.com/desired.php%3Fa%3Db%26c%3Dd ">
</script>

The URL of the PHP software with the desired content needs to be encoded only in the special case when it contains a "?" character.

Other Reasons and Ways To Use gateway.php

The gateway.php script can be used with JavaScript for publishing PHP software output.

It can also be used to publish content that should not be indexed by search engines nor followed by any spiders. Ads, for example. Or content unrelated to the topic of the web page, like a link to a neighbor's garage sale page.

The gateway.php software can grab content from any public URL. The URL doesn't have to be to another PHP script. Thus, even text files can be imported with a JavaScript script tag.

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC