150
function loadJSON(url) { var headID = document.getElementsByTagName("head")[0]; var newScript = document.createElement('script'); newScript.type = 'text/javascript'; newScript.src = url; headID.appendChild(newScript);}
http:/Graph.facebook.com/?id=https:/speckybel.com
{ "id": "https://speckyboy.com", "shares": 608}
http:/Graph.facebook.com/?id=https:/speckybel.com&callback=doThisFunction
/**/ doThisFunction({ "id": "https://speckyboy.com", "shares": 608});
function doThisFunction(data){ alert('the page ' + data.id + ' has been shared ' + data.shares + ' times');}
<?php/** * Template Name: REST Blog Listing * @package WordPress * */// Notice that there is no request to get_header(); it is not needed/* * REST Class from speckyboy * */// check if the class exists already as we don't want it to failif(!class_exists('speckyboyREST')){ class speckyboyREST{ // define class variabels protected $_callback; protected $_count; protected $_output; protected $_format; // automate everything public function __construct(){ $this->_output = array(); // setup the output as an array, needed for json $this->_count = 10; // define the blog count shown as default $this->_callback = false; // set callback to false just incase a jsonp callback request is not made $this->_format = 'json'; // default to json replies, will be changed if callback used if( $this->check_url_vars() ){ $this->save_url_vars(); } $this->make_the_loop(); $this->send_output(); } protected function check_url_vars(){ if(!empty($_GET)){ // check to see if the GET variables are set at all, this could more specific if wanted. return true; }else{ return false; } } protected function sanatize_vars($var){ // stop nasties by removing them from the possible URL vars return strip_tags(html_entity_decode(urldecode($var))); } protected function save_url_vars(){ if(isset($_GET['callback']) ){ $this->_format = 'jsonp'; // as there is a callback, setting the output to be jsonp $this->_callback = $this->sanatize_vars($_GET['callback']); // defining the output } if(isset($_GET['count']) ){ $this->_count = $this->sanatize_vars($_GET['count']); // could use is_numeric here if wanted } } protected function error($type, $errorMessage){ $this->_output['status'] = $type; // define the error message value $this->_output['data'] = $errorMessage; // define the error message text $this->_format = 'jsonp'; // setting to jsonp so that we can force a callback to a error handler $this->_callback = 'errorReply'; // will send errors back to errorReply function. } protected function make_the_loop(){ $query = array( 'post_type' => 'post', // get only posts 'post_status' => 'publish', // only published stuff 'posts_per_page'=>$this->_count, // with the page count wanted count = -1 means everything ); $loop = get_transient('restget'.$this->_count); // get the transient of the loop to make things faster if($loop === false){ $loop = new WP_Query($query); // make loop query set_transient('restget'.$this->_count, $loop, 60 * 60); // set the transient for the loop for 1 hour } $array_out = array(); // setup an array to save the results if ($loop->have_posts()) : while ($loop->have_posts()) : $loop->the_post(); // do the loop if(function_exists('has_post_thumbnail') && has_post_thumbnail()){ // check for a post thumnail $image = get_the_post_thumbnail($page->ID, 'thumbnail'); }else{ $image = null; // set image to null is there is not one. } $array_out[] = array( // add a new array set 'title' => get_the_title(), // the title 'permalink' => get_permalink(), // the link back to the main site 'excerpt' => get_the_excerpt(), // the post excerpt 'image'=> $image ); endwhile; // finsh the loop and then save the data $this->_output['status'] = '200'; // set a good status $this->_output['data'] = $array_out; // add the array to the data ready to json encode else: $this->error('400','no data returned'); // no data error endif; } protected function send_output(){ $time = time() + 60 * 60; // setup a 1 hour cache on the output $expire = date ( "D, d M Y H:i:s e", $time ); // define the expire time header("Expires: " . $expire ); // set expire header for 1 hour header("Content-type: application/json"); // set content type to be json if(function_exists('ob_gzhandler')){ // setup GZIP compression if available ob_start('ob_gzhandler'); //start gzipped buffer } else{ ob_start(); // start output buffer } switch($this->_format){ case 'json': echo json_encode($this->_output); // if json, echo json encoded data to buffer break; case 'jsonp': echo $this->_callback."(".json_encode($this->_output).");"; // if jsonp, echo json encode with callback to buffer break; } ob_end_flush(); // flush the buffer sendin the output to the client } public function __desctuct(){ unset($this); // kill all the things that have been made, clean up behind. } }}// Start the class on load and get reply$restapi = new speckyboyREST();// notice no request for get_sidebar() or get_footer(); it is not needed as we are not sending HTML, just JSON?>
function callbackFunction(data){ values = data.data; var output = '<ul>'; for (var i = 0; i < values.length; i++) { output += "<li style='clear:left'>"; if( values[i].image != null) { output += "<div style='float:left; margin-right:10px'>" output += values[i].image; output += "</div>" } output += "<h2><a href='"+ values[i].permalink +"'>" + values[i].title + "</a></h2>"; output += "<p>" + values[i].excerpt + "</p>"; output += "</li>"; } output += '</ul>'; document.getElementById('output').innerHTML = output;}
function errorReply(data){ alert('there has been a problem. ' + data.data);}
wp-config.php
最新文章
随机推荐