Archive for category PHP
Web Service using JSONP from External Domain – PHP – JQuery
Posted by dan in Code, Javascript, PHP, jQuery on February 3, 2012
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>
PHP Webservice that uses JSON PDO – Accessed by JQuery AJAX
Posted by dan in Javascript, PHP, jQuery on January 6, 2012
You may want to look at this previous post where I setup a Web Service using JSONP from External Domain – PHP – JQuery
The HTML that calls the Javascript:
<div id="taglist"></div> <input name="searchTags" id="searchTags" type="text" /> <input type="button" name="searchtags" onclick="getTags()" value="search"/> <div id="tagresult"> </div>
The JavaScript (JQuery) AJAX Request and Response Processing:
function getTags() { var q = $('#searchTags').val(); $.getJSON('ws.php?type=tag&wN=name&wO=like&wV=' + q, function(data) { var items = []; var htmlResult = ""; $.each(data, function(key, val) { //As you can see, you can access the JSON (at the bottom of the page) by doing val.PROPERTYNAME: htmlResult += "<br />Add : <a href=\"#\" onclick=\"addTag("+ val.id +", '"+ val.getfullname +"'); return false;\">"+ val.getfullname +"</a>"; }); $('#tagresult').html(htmlResult); }); }
PHP Code that returns JSON in the PHP file called by the AJAX: (ws.php)
$ta = new TagAccess(); $tarray = $ta->GetTagsWhere(" {$whereName} LIKE '%{$whereVariable}%'"); header('Content-type: application/json'); echo "", json_encode($tarray), "";
The GetTagsWhere Helper Function:
public function GetTagsWhere($where) { $retArray; $sql = "SELECT * FROM tags WHERE {$where}"; // THE PDO Object was created previously foreach ($this->pdo->query($sql) as $row) { $retArray[] = $this->GetTag($row); } return $retArray; }
JSON Returned:
[{"id":"4","name":"FICTION","parentid":"3","getfullname":"BOOKS > FICTION"},{"id":"5","name":"NON-FICTION","parentid":"3","getfullname":"BOOKS > NON-FICTION"}]
Simple PHP Pseudo Proxy

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
Facebook Website Integration – HTML
Posted by dan in Code, Javascript, PHP, jQuery on May 25, 2011

To integrate Facebook on to a website without the use of a facebook app you need these things:
- Facebook Page RSS Feed (can be tricky)
- Feedburner Account (free,easy) (here)
Once you have both of those things you can do this in feedburner:
- Add a new feed (your facebook url)
- Go to the publicize tab
- Go to BuzzBoost
- Sroll down and fill out the form, you will be given html code.
-
<script src="http://feeds.feedburner.com/DanielFolkessFacebookStatusUpdates?format=sigpro" type="text/javascript" ></script> <noscript><p>Subscribe to RSS headline updates from: <a href="http://feeds.feedburner.com/DanielFolkessFacebookStatusUpdates"></a> <br/>Powered by FeedBurner</p> </noscript>
- Preview:
And that’s it!
You can now style it up a little bit with some CSS and stuff.
PHP API – Google Calendar – Updating Visibility
The Privacy / Visibility Part:
$event->visibility->value = "http://schemas.google.com/g/2005#event.private";
Here it is in context: Read the rest of this entry »
Pitchfork 'Best New Albums' Torrent Search
Ok so this might be slightly illegal, but I am really just generating some links.
I wrote some code that will take the RSS feed from this page: http://pitchfork.com/reviews/best/albums/ and searches BTJunkie for torrents.
Here it is in action:
http://danfolkes.com/pitchfork_torrent_albums_best/
Here is the source: (also here)
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 | <?php echo "<table border=1>"; $doc = new DOMDocument(); $doc->load('http://feeds2.feedburner.com/PitchforkBestNewAlbums'); foreach ($doc->getElementsByTagName('item') as $node) { echo "<tr><th colspan='2'>"; $title = $node->getElementsByTagName('title')->item(0)->nodeValue; echo $title; $doc2 = new DOMDocument(); $doc2->load("http://btjunkie.org/rss.xml?query=".urlencode($title)); echo "</th></tr>"; foreach ($doc2->getElementsByTagName('item') as $node2) { echo "<tr><td></td><td>"; $link = "<a href='".$node2->getElementsByTagName('link')->item(0)->nodeValue."' target='_blank'>"; $link.= $node2->getElementsByTagName('title')->item(0)->nodeValue."</a>"; echo $link; echo "</td></tr>"; } } //print_r($arrFeeds); echo "</table>"; ?> |
PHP Base64_Encode – Intentionally screwing up pictures
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 } ?> |
PHP Gmail Imap Connection with Database Insert

Using PHP, We will connect to a Gmail mailbox and pull down the last message headers and to and from email addresses.
With this data, we will insert it into a MySQL Database and then delete the message from the IMAP Server.
LET ME KNOW IF THIS HELPED YOU IN THE COMMENTS! Enjoy!
Here is the code:
Read the rest of this entry »
TikiWiki – Tracker Email Subject Improvement-Modification
The template for the Tracker email is here:
\tikiwiki\templates\mail\tracker_changed_notification_subject.tpl
I changed it to this so that it would improve the way it looks in emails:
{$mail_trackerName}{tr} - #{/tr}{$mail_itemId}{tr} modified.{/tr}
IP Locator Webservice – PHP – Ipmap – Command Line
This is similar to my Python script here:
http://danfolkes.com/index.php/2009/04/29/ipmapcom-python/
It uses this sites service to pull the location of each user:
http://www.ipmap.com/
It outputs in XML, Plain Text, and HTML.
Fields:
- ip
- hostname
- ipreverse
- country
- region
- city
- blacklist
- gmaps
HERE IS THE LINK TO THE WEB SERVICE SITE:
http://www.danfolkes.com/ipmaps/
CWS – Chord Web Service
I am creating a web service for musicians that will allow them to reference an abundance of musical references on the fly using AJAX.
Firstly, it will deliver chords and fingerings of chords for guitars to the users websites (think Google Maps).
My goal is to enhance the web.
Here is the site: http://cws.danfolkes.com
It’s still in development, but it’s getting close to launch.
Let me know what you think (only if it’s nice)



