PHP Sample Code Update - BETA

1 post / 0 new
puravida's picture
puravida
Jedi Warrior
Offline
Joined: 09/01/2007
Visit puravida's Website

UPDATE (03/11/2012): The code below has been updated, overhauled, and tested fully. It has been released as version 2.0.0 of the STW PHP sample code, which can be found on the "PagePix Plugins" page.

Below you will find the BETA sample code changes for the shrinktheweb function. This code is meant to provide an easier, alternate means to pull out XML fields from the response, check for various error conditions, and then update a database (if present) with troubleshooting information. It has not been fully tested (i.e. BETA) and we will only be able to provide limited support at this time.

/**********************************************************************
	Let's support a more flexible file naming convention
**********************************************************************/
if($full)		{$prehash.='a'.$stwxmax.'x'.$stwymax;}						// full-length request (can't also be custom)
else			{$prehash.='c'.$stwxmax.'x'.$stwymax;}						// request by capture size ($prehashqry not need if not full and not cust)
if($nrx)		{$prehash.='b'.$stwnrx.'x'.$stwnry;}						// native resolution request (w/ custom = widescreen capable)
$hash		=	md5($prehash.$stwqual.$stwurl);								// feature-set hash value
/**********************************************************************
	Retrieve the XML response
**********************************************************************/
if (extension_loaded('simplexml')) { // If simplexml is available, we can do more stuff!
	$dom = new DOMDocument;
	$line_xml = DOMDocument::loadXML($line);
	$xml = simplexml_import_dom($line_xml);
	// Pull response codes from XML feed
	$stw_response_status	= $xml->children('https://shrinktheweb.com/doc/stwresponse.xsd')->Response->ResponseStatus->StatusCode; // HTTP Response Code
	$stw_action				= $xml->children('https://shrinktheweb.com/doc/stwresponse.xsd')->Response->ThumbnailResult->Thumbnail[1]; // ACTION
	$stw_response_code		= $xml->children('https://shrinktheweb.com/doc/stwresponse.xsd')->Response->ResponseCode->StatusCode; // STW Error Response
	$stw_last_captured		= $xml->children('https://shrinktheweb.com/doc/stwresponse.xsd')->Response->ResponseTimestamp->StatusCode; // Last Captured
	$stw_quota_remaining	= $xml->children('https://shrinktheweb.com/doc/stwresponse.xsd')->Response->Quota_Remaining->StatusCode; // New Reqs left for today
	$stw_category_code		= $xml->children('https://shrinktheweb.com/doc/stwresponse.xsd')->Response->CategoryCode->StatusCode; // Not yet implemented
	$thumbnail				= $xml->children('https://shrinktheweb.com/doc/stwresponse.xsd')->Response->ThumbnailResult->Thumbnail[0]; // Image Location (alt method)
	
	if($stw_action == 'fix_and_retry'){$invalid=1;}else{$invalid=0;}
	if($stw_action == 'noretry'){$error=1;}else{$error=0;}
	
	if($db){ // set $db=1 if using a database (sample db structure available for download at https://shrinktheweb.com/uploads/path_shrinkwebdb.zip
		mysql_query("UPDATE stw_requests SET 
			timestamp='".mktime()."', capturedon = '".$stw_last_captured."', invalid = '".$invalid."', 
			stwerrcode = '".$stw_response_code."', error = '".$error."', errcode = '".$stw_response_status."'
		WHERE hash = '".$hash."'");
		if(mysql_affected_rows() == 0){ // doesn't exist
			mysql_query("INSERT INTO stw_requests SET 
				domain = '".$stwurl."', hash = '".$hash."', timestamp='".mktime()."', capturedon = '".$stw_last_captured."', 
				quality = '".$stwqual."', full = '".$stwfull."', xmax = '".$stwxmax."', ymax = '".$stwymax."', 
				nrx = '".$stwnrx."', nry = '".$stwnry."', invalid = '".$invalid."', stwerrcode = '".$stw_response_code."', 
				error = '".$error."', errcode = '".$stw_response_status."'");
		}
	}
	if($thumbnail){$exists = "true";}else{$exists == "false";} // we only get a location if it exists
}
else{
	// LEGACY SUPPPORT
	if(substr_count($line,"<stw:StatusCode>0</stw:StatusCode>")>0){$stw_quota_remaining = 0;} // legacy support for limit detection
	// Pull out thumbnail (legacy method) and exists TRUE or FALSE
	$num_matches = preg_match('/<[^:]*:Thumbnail\\s*(?:Exists=\"((?:true)|(?:false))\")?[^>]*>([^<]*)<\//', $line, $matches);
	if($num_matches){
		$exists = $matches[1];
		$thumbnail = $matches[2];
	}
}
if($stw_quota_remaining == 0){ // Over the daily/monthly limit...
	mysql_query("UPDATE stw_admin SET trystamp='".mktime()."' WHERE id=0");
	$image="/home/".HMDIR."/public_html/images/leech-sm.jpg";
	@imagejpeg(@imagecreatefromjpeg($image), NULL, 90);
}

If you would like advanced logging features, download the sample ShrinkTheWeb Database.
Note: Link removed in lieu of the newer release, version 2.0.0 that is available now.

ShrinkTheWeb® (About STW) is another innovation by Neosys Consulting
Contact Us | PagePix Benefits | Learn More | STW Forums | Our Partners | Privacy Policy | Terms of Use

Announcing Javvy, the best crypto exchange and wallet solution (coming soon!)

©2018 ShrinkTheWeb. All rights reserved. ShrinkTheWeb is a registered trademark of ShrinkTheWeb.