Ping Sample Code
(Note: if you have sample code you'd like to share for another language, contact us , and we can either host it here, or
link to your site.)
PHP Sample Code
This code requires either that fopen() supports "allow_url_open", or that sockets (in particular fsockopen()) are working on your php installation.
You may execute the test code inside this class:
$pinger = new PingPhotoPing();
$pinger->test();
or see below for a description of how to call this class.
You may prefer to
download this code (do a "View Source" on the page after clicking this link).
<?php
/*
Ping PhotoPing.com from PHP.
Version v1.0.
Created by Frank Leahy, January 2007.
You can call this class with just the three minimum required parameters, like this:
$params = array();
$params['type'] = 'photo';
$params['title'] = 'Nathaniel with ice cream';
$params['webPageUrl'] = 'http://cornwall.backtalk.com/photos/cornwall/?show=photo&album=Cornwall%20Weblog&photoNum=125';
$pinger = new PingPhotoPing();
$pinger->ping($params);
Or you can call it with any of the other 17 optional parameters by
bundling the parameters into the "$params" array like this:
$params = array();
$params['type'] = 'photo';
$params['title'] = 'Nathaniel with ice cream';
$params['webPageUrl'] = 'http://cornwall.backtalk.com/photos/cornwall/?show=photo&album=Cornwall%20Weblog&photoNum=125';
$params['thumbnailUrl'] = 'http://cornwall.backtalk.com/images/Nathaniel_thumb.jpg';
$params['thumbnailWidth'] = 110;
$params['thumbnailHeight'] = 110;
$params['url'] = 'http://cornwall.backtalk.com/images/Nathaniel.jpg';
$params['width'] = 800;
$params['height'] = 533;
$params['subject'] = 'Nathaniel';
$params['location'] = 'London, Thames Embankment';
$params['geoLocation'] = '51.508131,-0.110539';
$pinger = new PingPhotoPing();
$pinger->ping($params);
No copyright is claimed on this code. You may use this code
as is, you may change it, fix it, do with it as you wish.
The standard disclaimers apply "No warranties are made that
this code actually works in your environment, blah, blah, blah."
If you would like to donate ping code for other languages,
please post your code in the "Code Samples" support forum at
http://support.photoping.com/viewforum.php?f=4.
*/
class PingPhotoPing
{
private $host = 'ping.photoping.com';
private $path = '/ping.php';
// We recommend using POST if the request string is close to or over 2000 characters in length.
private $method;
public function PingPhotoPing($method = 'GET')
{
if ($method == 'POST')
$this->method = 'POST';
else
$this->method = 'GET';
}
// For internal testing only
public function changeHost($host)
{
$this->host = $host;
}
// All the work happens here
public function ping($params)
{
$requestStr = http_build_query($params);
if ($this->method == 'POST')
$reply = $this->pingUsingSocket($requestStr);
else
$reply = $this->pingUsingFOpen($requestStr);
return $reply;
}
private function pingUsingFOpen($requestStr)
{
$url = 'http://'.$this->host.$this->path.'?'.$requestStr;
$resource = fopen($url, 'r');
if ($resource !== false)
{
$result = fread($resource, 4096);
fclose($resource);
}
return $result;
}
private function pingUsingSocket($requestStr)
{
$contentLength = strlen($requestStr);
// Use HTTP/1.0 so we don't get chunked reply, and the connection is closed.
$request =
"POST {$this->path} HTTP/1.0\r\n".
"Host: {$this->host}\r\n".
"User-Agent: PingPhotoPing from PhotoPing.com Sample Code\r\n".
"Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: $contentLength\r\n\r\n".
"$requestStr\r\n";
$socket = fsockopen($this->host, 80, $errno, $errstr);
if (!$socket)
return "Error: socket error $errno '$errstr'";
$result = '';
fputs($socket, $request);
while (!feof($socket))
{
$result .= fgets($socket, 4096);
}
fclose($socket);
$array = explode("\r\n\r\n", $result);
if (count($array) > 1)
return $array[1];
else
return "Error: unable to read from socket";
}
public function test()
{
// Simplest possible ping
$params = array();
$params['title'] = 'Ice cream in London';
$params['webPageUrl'] = 'http://cornwall.backtalk.com/photos/cornwall/?show=photo&album=Cornwall%20Weblog&photoNum=125';
$reply = $this->ping($params);
print("<b>Test simple ping reply (reply is xml):</b><pre>");
print_r(htmlspecialchars($reply));
print("</pre>\n\n");
// Ping with all parameters
$params = array();
$params['type'] = 'photo';
$params['webPageUrl'] = 'http://cornwall.backtalk.com/photos/cornwall/?show=photo&album=Cornwall%20Weblog&photoNum=125';
$params['url'] = 'http://cornwall.backtalk.com/images/Nathaniel.jpg';
$params['width'] = '800';
$params['height'] = '533';
$params['thumbnailUrl'] = 'http://cornwall.backtalk.com/images/Nathaniel_thumb.jpg';
$params['thumbnailHeight'] = 110;
$params['thumbnailWidth'] = 110;
$params['date'] = 'Sept 11, 2004';
$params['location'] = 'London, Thames Embankment';
$params['geoLocation'] = '51.508131,-0.110539';
$params['title'] = 'Ice cream in London';
$params['subject'] = 'Nathaniel, ice cream cone';
$params['description'] = 'Eating ice cream along the Thames, on Kristyna\'s last day';
$params['tags'] = 'Nathaniel, ice cream cone, Kristyna';
$params['credit'] = 'Frank Leahy';
$params['commercial'] = 'false';
$params['ccLicense'] = 'by-nc-nd';
$params['forSale'] = 'true';
$params['adult'] = 'false';
$reply = $this->ping($params);
print("<b>Test complex ping reply (reply is xml):</b><pre>");
print_r(htmlspecialchars($reply));
print("</pre>\n\n");
}
}
?>