If you use PHP to deliver web content, you can embed a PopChart image using the PHP version of the PopChart Embedder.
For the most part, the code that you will use for embedding a PopChart image with a PHP script is exactly the same as the code you created for the JavaScript embedder in Chapter 4. This section will explain the major differences. Example 5.11 at the end of this section shows you how the web page from Example 4.7 would look using PHP.
Note: This documentation assumes that if you want to embed a PopChart image in an PHP Page, you already know how to use PHP. If this is not the case, you may wish to brush up on PHP before continuing.
Before you can include the PopChart Embedder library into a PHP script, you must first make sure that the PopChartEmbedder.php file, located in the dev_tools/php_embedder folder, is accessible to your PHP scripts. Usually the easiest way of making them accessible is to copy them to the same folder as the scripts that will use the PopChart Embedder. Alternatively, you can copy the PopChartEmbedder.php file to a shared or includes directory that is accessible to your PHP server.
To include the PopChart Embedder library in a PHP script, include the following line at the beginning of your script.
require("PopChartEmbedder.php");
This instructs the PHP compiler to load the PopChartEmbedder class.
To instantiate a PopChart Embedder object, you should use the following line of code (this assumes that you want to name your object myPopChart).
$myPopChart = new PopChartEmbedder();
All PopChart Embedder code, including the require statement, should be delimited by <?php and ?>, as in the following code segment:
$myPopChart->appearanceFile = "apfiles/line.pcxml";
$myPopChart->loadPCXML("http://myserver.com/?graph1");
As mentioned in "Setting the Server Information" in Chapter 4, non-JavaScript versions of PopChart Embedder require you to tell them the location of PopChart Server. You need to set two values: the address that the web client will use to access PopChart Server ( externalServerAddress), and the address that PopChart Embedder uses to access PopChart Server ( internalCommPortAddress).
These are not the same addresses. First of all, PopChart Embedder uses a different port to communicate with PopChart Server. This port is known as the comm port, and, unless you change it, is set to 2002. Second of all, if you are using a firewall or a redirector, PopChart Server's address will be different for the web client than for the PopChart Embedder.
For example, if you are running PopChart Server at 10.0.1.1, but your web clients request images from http://popchart.mycompany.com, you need to include the following lines of code in the code that generates your PopChart image:
$myPopChart->externalServerAddress = "http://popchart.mycompany.com:2001";
$myPopChart->internalCommPortAddress = "10.0.1.1:2002";
When you use the JavaScript PopChart Embedder, it automatically tells PopChart Server what the web client's user agent is. However, server-side PopChart Embedders can only tell PopChart Server a client's user agent if you specifically pass this information on to PopChart Embedder using the userAgent attribute. This information is significant because if PopChart Server knows what browser a client is using, it can return optimized (i.e. shorter and without JavaScript) HTML for embedding the PopChart image.
Server-side PopChart Embedders can only tell PopChart Server a client's user agent if you specifically pass this information on to PopChart Embedder using the userAgent attribute. The following line of code shows how to do this in a PHP page.
$myPopChart->userAgent = $HTTP_SERVER_VARS['HTTP_USER_AGENT'];
It is not necessary to tell PopChart Embedder the client's user agent; however, you will not be able to take advantage of optimized HTML if you do not do this.
There are certain PopChart Embedder methods that are unavailable in the PHP version. These are as follows:
In PHP, you will always use pointer notation when calling methods or accessing attributes. This differs from the dot-notation that is used in most of the examples in this documentation. Also, variable names must always begin with a dollar sign.
For example, consider the following command in Java.
myPopChart.appearanceFile = "bar.pcxml";
The equivalent code in PHP would be as follows:
$myPopChart->appearanceFile = "bar.pcxml";
For most PHP developers, this will be an obvious conversion process.
When you are ready to actually write your embedding HTML to the web page, you will need to output the getEmbeddingHTML() method. To do this, use the echo statement, as shown below.
echo $myPopChart->getEmbeddingHTML();
The following web page uses the PHP PopChart Embedder to produce exactly the same results as the page you created in Chapter 4.
Example 5.11 Embedding a PopChart Image with PHP
<title>My First Embedded PopChart</title>
<h1>This is a PopChart Image</h1>
<!-- insert PopChart image here -->
require("PopChartEmbedder.php");
$myPopChart = new PopChartEmbedder();
$myPopChart->externalServerAddress="localhost:2001";
$myPopChart->internalCommPortAddress = "localhost:2002";
$myPopChart->appearanceFile = "examples/apfiles/bar.pcxml";
$myPopChart->imageType = "FLASH";
$myPopChart->fallback = "STRICT";
$myPopChart->pcScript = "title.setText(Hello World)";
$myPopChart->userAgent = $HTTP_SERVER_VARS['HTTP_USER_AGENT'];
echo $myPopChart->getEmbeddingHTML();
<p>If you see an image above, congratulations. You have successfully embedded your first PopChart image.</p>