Identity and Access Management Consultant
IT Search
US-TX-Dallas

Justtechjobs.com Post A Job | Post A Resume

[New Guys Helper] Simple Remoting, Part 2

Now that we understand the basic premise, we can try a slightly more difficult script. We will not be using RegEx for this one, but rather array and string functions. The reason for this is that it is easier due to the output. We are going to end up with:

Now we know exactly what we're going to get, and how we want it to look. With that, we need to know what the content from the original web page looks like. To do that we must visit: http://www.dhs.gov/dhspublic/getAdvisoryCondition. The output that you should see looks like this:

Threat Level XML:

<?xml version="1.0" encoding="UTF-8"?>
<THREAT_ADVISORY CONDITION="ELEVATED" />

First, we grab the information just as we did in the previous example. Next we will split the contents into an array based on lines. Then we'll select just the portion from the "=" sign on. After that, we'll go ahead and get the HS threat level. Once we've got the threat level, it's just a matter of displaying it as we wish.

Threat Level Code:

<?php
	$url = 'http://www.dhs.gov/dhspublic/getAdvisoryCondition';
	$handle = fopen($url, "r");
	$contents = file_get_contents($url);
	fclose($handle);

	$contents = explode("\n", $contents);
	$contents = $contents[1];
	$level = strstr($contents, '="');
	$level = substr($level, 2, -5);

	switch($level){
		case 'LOW':
			$color = '#090';
			break;
		case 'GUARDED':
			$color = '#069';
			break;
	             case 'ELEVATED':
			$color = '#fc0';
			break;
		case 'HIGH':
			$color = '#f90';
			break;
		case 'SEVERE':
			$color = '#900';
			break;
	}

	echo '<span style="color:'.$color.'; font-weight: bold;">'.$level.'</span>';
?>

To help you better understand what I did with the array and string functions, I've gone ahead and color coded the steps. Each step has an explanation. Feel free to look up the functions in the php manual as well.

Threat Level XML:

<?xml version="1.0" encoding="UTF-8"?> <THREAT_ADVISORY CONDITION="ELEVATED" />

The line in this color is taken out via splitting it into an array

PHP Code:

$contents = explode("\n", $contents); $contents = $contents[1];

This section is removed via the strstr() function. We select only the text from the equals-quote combo (=") on.

PHP Code:

$level = strstr($contents, '="');

This section is selected with the substr() function. We select the 3rd through 5th from the end character span. In this example, it is E through E.

PHP Code:

$line = substr($level, 2, -5);

It's done--that's all there is to it!! Thus ends this brief introduction to remoting with PHP. You've learned to grab specific data from an external web page, format that data and display it on your own web page!

Happy PHPing!!



Comments:

No Messages Found

You can post questions/corrections/feedback here

 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.