PhpFiddle APIs version 3.0

APIs with JSON response support W3C recommends XMLHttpRequest specification: Cross-Origin Resource Sharing, which defines a mechanism to enable client-side cross-origin requests. All Ajax requests can use these APIs to access PhpFiddle's features directly

In the following text code_id is a link id or gist id which generated by PhpFiddle from saving the code. link id is made up of '-' and 6 or 8 characters in lowercase, for example, dfi-q1d, ze5q-kpb9, and gist id is string in 7 or 20 characters defined by GitHub Gist, for example: 3691666, 3682451, ec165a789941a37e51d8

When using API from browser address, all "&" and "#" in values of parameter should be replaced with URL encoded characters "%26" and "%23" respectively if existed, for example, "http://phpfiddle.org/api/hash?algorithm=md5&data=abcd#1234" has to be "http://phpfiddle.org/api/hash?algorithm=md5&data=abcd%231234"

 

1. Core APIs

NAME Runs Code
DESCRIPTION Executes posted code and returns wrapped execution results in JSON
URL STRUCTURE http://phpfiddle.org/api/run/code/json
VERSION 2
METHOD POST
PARAMETER One pair of 'code' and non-empty value in query data is required, and multiple other name-value pairs in query data are available

If 'phptag=yes' pair existed in query data PhpFiddle would add '<?php ' and ' ?>' to value of 'code' at beginning and ending respectively
 
RESPONSE JSON in format: {"result" : Execution result JSON}
ERROR JSON in format: {"error" : "Error describing message"}
NOTE PhpFiddle wraps execution results in JSON automatically, no JSON encoding reqired in executed code. If JSON encoded data was output by executed code, decoding response result on the page, for example, JSON.parse(data.result)

 

NAME Gets raw contents from URL
DESCRIPTION Gets raw contents with CSS selector from code URL and returns wrapped contents in JSON
URL STRUCTURE http://phpfiddle.org/api/raw/url/json
VERSION 2
METHOD GET and POST
PARAMETER Pair of 'url=valid_URL' in query data is required

If 'selector=CSS_selector' pair existed in query data PhpFiddle would extract text contents with CSS selector from the page then return result JSON, and if 'selector=CSS_selector' and 'innerhtml=yes' pairs existed at the same time in query data PhpFiddle would extract HTML contents instead of text contents from the page
 
RESPONSE JSON in format: {"code" : Code contetns JSON}
ERROR JSON in format: {"error" : "Error describing message"}

 

NAME Gets raw contents from code_id
DESCRIPTION Gets raw code contents from code_id and returns wrapped code contents in JSON
URL STRUCTURE http://phpfiddle.org/api/raw/code_id/json
VERSION 2
METHOD GET and POST
PARAMETER None
RESPONSE JSON in format: {"code" : Code contetns JSON}
ERROR JSON in format: {"error" : "Error describing message"}

 

NAME Searches code
DESCRIPTION Search code from PhpFiddle's code repository with search keywords in code description and returns matched data with JSON format, and allow 'and' operator for keywords
 
URL STRUCTURE http://phpfiddle.org/api/search
VERSION 2
METHOD GET and POST
PARAMETER Pair of 'keywords=non_empty_value' in query data is required
RESPONSE JSON in format: {"links" : matched data JSON}
Sample return value

	{"links" : 
	 [
	   {
		  "id" : "nhr-juv",
		  "type" : "public",
		  "description" : "PHP GD line graph"
	   },
	   {
		  "id" : "3682451",
		  "type" : "gist",
		  "description" : "3D pie chart with GD library, code on github:gist"
	   }
	 ]
	}
				
ERROR JSON in format: {"error" : "Error describing message"}
Example

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<script type="text/javascript" src="/js/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="/js/phpjs/section.min.js"></script>

<script type="text/javascript">
	$(function(){
	   $.ajax({
		  url: "http://phpfiddle.org/api/search",
		  type: "POST",
		  data: "KeyWords=phpfiddle",
		  cache: false,
		  dataType: "json",
		  success: function(data) {
				   if (data.links) {
					  document.write("success");
					  dump(data);
				   } 
				   if (data.error) {
					  document.write(data.error);
				   }
			}
	   });
	});
</script>
</head>
<body>

</body>
</html>
		

 

NAME Extracts multi-part text from a web page
DESCRIPTION Extracts multi-part text from a web page with CSS selectors
URL STRUCTURE http://phpfiddle.org/api/acm/json
VERSION 2
METHOD GET and POST
PARAMETER "url=valid_page_url{&postdata=para1=val1%26para2=val2%26...}
&selector[1]=CSS_selector_1&type[1]=html_or_text
{&selector[2]=CSS_selector_2&type[2]=html_or_text{&selector[3]=...}}"

Parts between "{" and "}" are optional, "postdata=para1=val1%26para2=val2%26..."
is for sending data to "valid_page_url" with HTTP POST method, and
all character "&" and "#" in values of "url", "postdata" and "selector[n]"
( n is a non zero positive integer ) would be replaced by "%26" and "%23"
respectively if existed
 
RESPONSE JSON in format: {"result" : extracted data JSON}
ERROR JSON in format: {"error" : "Error describing message"}

 

NAME Hash
DESCRIPTION Generate a hash value in lowercase hexadecimal digit, supported algorithms include "md2", "md4", "md5", "sha1", "sha224", "sha256", "sha384", "sha512", "ripemd128", "ripemd160", "ripemd256", "ripemd320", "whirlpool", "tiger128,3", "tiger160,3", "tiger192,3", "tiger128,4", "tiger160,4", "tiger192,4", "snefru", "snefru256", "gost", "adler32", "crc32", "crc32b", "salsa10", "salsa20", "haval128,3", "haval160,3", "haval192,3", "haval224,3", "haval256,3", "haval128,4", "haval160,4", "haval192,4", "haval224,4", "haval256,4", "haval128,5", "haval160,5", "haval192,5", "haval224,5" and "haval256,5"
 
URL STRUCTURE http://phpfiddle.org/api/hash
VERSION 2
METHOD GET and POST
PARAMETER Pairs of "algorithm=supported_algorithm" and "data=message_to_be_hashed" in query data are required
RESPONSE JSON in format: {"result" : "lowercase hexadecimal digit"}
ERROR JSON in format: {"error" : "Error describing message"}

 

 

2. key-value storage APIs

key-value storage APIs provide key-value storage services, the specifications of key-value storage APIs include: Key-values are saved on server, there is no concepts of time to live, all key-values in storage are permanent. The key is 1 to 60 characters in length and case-insensitive, the value 1 to 255 characters in length and case-sensitive. Key-value storage APIs except getpublic are working only for logged-in user

NAME Get
DESCRIPTION Gets corresponding value of the key
URL STRUCTURE http://phpfiddle.org/api/storage/get
VERSION 2
METHOD GET and POST
PARAMETER Pair of "key=string_in_1_to_60_characters" in query data is required
RESPONSE JSON in format: {"value" : "Corresponding value of the key"}
ERROR JSON in format: {"error" : "Error describing message"}

 

NAME Getpublic
DESCRIPTION Gets corresponding value of the key in public
URL STRUCTURE http://phpfiddle.org/api/storage/getpublic
VERSION 2
METHOD GET and POST
PARAMETER Pair of "key=string_in_1_to_60_characters" in query data is required, and pair of "response=json" is optional
RESPONSE If pair of "response=json" in query data HTTP response with JSON in format: {"value" : "Corresponding value of the key"} else HTTP response with "Corresponding value of the key"
 
ERROR If pair of "response=json" in query data HTTP response with JSON in format: {"error" : "Error describing message"} else HTTP response with "Error describing message"
 
Note Logged-in isn't needed for using this API

 

NAME Getall
DESCRIPTION Gets maximum 256 key-values from an offset in a storage
URL STRUCTURE http://phpfiddle.org/api/storage/getall
VERSION 2
METHOD GET and POST
PARAMETER Optional, for pair of "section=n" in query data API returns maximum 256 key-values starting from row (n-1) * 256 in a storage, default section is 1
 
RESPONSE JSON in format: {"result" : Execution result JSON}
ERROR JSON in format: {"error" : "Error describing message"}
Note Maximum valid section for a storage is Math.ceil( storage count / 256 ) in JavaScript

 

NAME Count
DESCRIPTION Count all key-values in a storage
URL STRUCTURE http://phpfiddle.org/api/storage/count
VERSION 2
METHOD GET and POST
PARAMETER None
RESPONSE JSON in format: {"result" : integer}

 

NAME Set
DESCRIPTION Set key-value to a storage, if the key exists in the storage the value would be replaced by new value, otherwise insert new key-value pair to the storage
 
URL STRUCTURE http://phpfiddle.org/api/storage/set
VERSION 2
METHOD GET and POST
PARAMETER Pairs of 'key=string_in_1_to_60_characters' and 'value=string_from_1_to_255_characters' in query data are required
 
RESPONSE JSON in format: {"result" : "Success describing message"}
ERROR JSON in format: {"error" : "Error describing message"}

 

NAME Setpublic
DESCRIPTION Set key-value from private to public if the key exists in the storage
 
URL STRUCTURE http://phpfiddle.org/api/storage/setpublic
VERSION 2
METHOD GET and POST
PARAMETER Pair of 'key=string_in_1_to_60_characters' in query data is required
 
RESPONSE JSON in format: {"result" : "Success describing message"}
ERROR JSON in format: {"error" : "Error describing message"}

 

NAME Unsetpublic
DESCRIPTION Set key-value from public to private if the key exists in the storage
 
URL STRUCTURE http://phpfiddle.org/api/storage/unsetpublic
VERSION 2
METHOD GET and POST
PARAMETER Pair of 'key=string_in_1_to_60_characters' in query data is required
 
RESPONSE JSON in format: {"result" : "Success describing message"}
ERROR JSON in format: {"error" : "Error describing message"}

 

NAME Delete
DESCRIPTION Delete key-value from a storage
URL STRUCTURE http://phpfiddle.org/api/storage/delete
VERSION 2
METHOD GET and POST
PARAMETER Pair of 'key=string_in_1_to_60_characters' in query data is required
 
RESPONSE JSON in format: {"result" : "Success describing message"}
ERROR JSON in format: {"error" : "Error describing message"}

 

MySQL 5.6