Web Service using JSONP from External Domain – PHP – JQuery

You may want to look at this previous post where I setup a PHP Webservice that uses JSON PDO – Accessed by JQuery AJAX

OK, so this code will use php to generate the JSON code that will get delivered over the internet to the JSONP (JSON Padding) client on an external domain. This allows your webservice to be available too whoever wants to call it.

Here is the PHP Code: (Not all of it, but enough to get the idea)

//http://SERVER_domain.com/webservice.php
private function GetTriviaTag($row) {
	$t = new Tag();
	$t->id = $row['id'];
	$t->triviaid = $row['triviaid'];
	$t->tagid = $row['tagid'];
	return $t;
}
public function GetWhere($where) {
	$retArray;
 
	$sql = "SELECT * FROM trivia_tags WHERE {$where}";
	foreach ($this->pdo->query($sql) as $row)
	{
		$retArray[] = $this->GetTriviaTag($row);
	}
	return $retArray;
}
$where = " (1 = 1)";  // THIS WILL SELECT ALL
$tarray = GetWhere($where);
 
//OUTPUT JSON:
header('Content-type: application/json');
echo $_GET['callback'];  // <-- THIS IS IMPORTANT FOR EXTERNAL DOMAIN ACCESS!!!
echo "(" . json_encode($tarray) . ")";

And now, to access it from javascript in an external domain (non – SERVER_domain.com ):
http://CLIENT_domain.com/Test.html:

<html>
	<head>
		<title>Test</title>
		<script src="http://code.jquery.com/jquery-latest.js"></script>
	</head>
	<body>
		<div id="triviaQuestions"> loading... </div>
		<script>
			$(function() {
				// THE 'callback=?' in this request is IMPORTANT FOR EXTERNAL DOMAIN ACCESS!!!
				$.getJSON('http://SERVER_domain.com/webservice.php&callback=?', function(data) {
					var items = [];
					var htmlResult = "";
					$.each(data, function(key, val) {
						htmlResult += "<br />Q: "+ val.id +", '"+ val.question +"";
					});
					$('#triviaQuestions').html(htmlResult);
				});
			});
		</script>
	</body>
</html>

Simple PHP Pseudo Proxy

php proxy simple pseudo

This is the code for a simple php pseudo proxy. If you are blocked by a firewall and google cache isn’t working for some reason. You can use a url with this php file in it:

<?php
$filename = $_GET["url"];
if (isset($filename))
{
        $handle = fopen($filename, "rb");
        $contents = stream_get_contents($handle);
        print $contents;
        fclose($handle);
}
else
{
        echo "url";
?>
<form method="GET">
<input type="text" name="url" /><input type="submit"/>
</form>
<?php
}
?>

Then you can go to where the url is hosted and try something like this:

index.php?url=http%3A%2F%2Fwww.last.fm%2Fuser%2Fdanfolkes%2Fcharts%3Frangetype%3D6month%26subtype%3Dartists

PHP Base64_Encode – Intentionally screwing up pictures

A screwed up base64 image.  Replaced all of the 'da' with 'ha'
A screwed up base64 image. Replaced all of the 'da' with 'ha'

So, I was a little bored so I made some PHP code.

This is what it does:

  • Takes query string values
  • Pulls an image from the web
  • base64_encodes it.
  • Distorts the image.
  • Replaces some of the base64 string data with other string data.
  • Outputs the image in an img tag.

Hope this helps someone, let me know if it does IN THE COMMENTS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
	if ((isset($_GET['i']))&&(isset($_GET['f']))&&(isset($_GET['r'])))
	{
		$find = $_GET['f'];
		$replace = $_GET['r'];
		$type = "gif";
		print_r($_GET);
 
		if (isset($_GET['t']))
		{
			$type = $_GET['t'];
		}
		$imgfile = $_GET['i'];
		$contents = implode (file($imgfile));
		$start = base64_encode($contents);
 
 
		$start = str_ireplace($find, $replace, $start);
 
	echo '<img src="data:image/'.$type.';base64,' . $start . '" />';
	}
	else
	{
	?>
		Try: <a href='base64.php?i=http://ec.europa.eu/culture/media/programme/images/logos/02/02.gif17.gif&f=lo&r=ha&t=gif'>http://ec.europa.eu/culture/media/programme/images/logos/02/02.gif17.gif&f=lo&re=ha&t=gif</a>
	<?php
	}
 
?>