Convert page-content to array

Submitted by Steven Heynderickx on 12 Sep 2012 12:11
rating: 2

Description

WD-API is able to get the content of a page using methode pages.get_one. For some reasons it is interesting to have the content of a page that uses data forms not as one long string, but as an array with keys. This script is fully autonome, so it makes no use of libraries and it does just that. In order for this to work you need to:

  1. adjust the script: add your name and API key
  2. upload the php-file to your server and
  3. call for the script using your URL (to the php-file) with following variables : page and site.

If you use the embedded page-variables from wikidot a link to the script could be like this:
http://yourdomain.com/wikidotapi/content_array_api.php?site=%%site_name%%&page=%%fullname%%

Sample Code

<?php
//-----------------------------------------------------------------------------------------------
// Thanks to the help of Jerad Whitaker this script gets the content
// of a page with a template that uses dataforms and converts all
// the fields into an array with the field names as array keys.
// I pass along the $page (%%fullname%%) and the $site (%%site_name%%)
// via the URL
//-----------------------------------------------------------------------------------------------
 
//-----------------------------------------------------------------------------------------------
// General settings
//-----------------------------------------------------------------------------------------------
$key="YOUR KEY HERE";
$username="YOUR USERNAME HERE";
$page=$_GET['page'];
$site=$_GET['site'];
 
$returnarray=getPage($site,$page,$username,$key);
 
$content = $returnarray[content]; //copies string to a new string var
$newarray = array();
print($content."<hr>");
$stringar = explode("\n",$content ); //split the content-string into an array of strings 
foreach($stringar as $data)
    { 
    $data_ar = explode(":", $data, 2); //split the strings into form fields and form data
    $key = $data_ar[0];
    $cont = trim(trim($data_ar[1]),"'\""); //trimming off quotes and spaces
    if($key == '' && $$cont == '') ; //skip empty strings
    else $newarray[$key] = $cont;
    } 
var_dump($newarray);
 
//----------------------------------------------------------------------------------------
    function getPage($site,$page,$username,$key)                       
//----------------------------------------------------------------------------------------
    {                                                                         
    $requestarray=array(    
            'site'=>$site,                                    
            'page'=>$page);                                   
 
    $request = xmlrpc_encode_request("pages.get_one",$requestarray);          
    $context = stream_context_create(array('http' => array(                   
            'method' => "POST",                                               
            'header' => "Content-Type: text/xml",                             
            'content' => $request                                             
            )));                                                              
    $file = file_get_contents("https://".$username.":".$key.                  
    "@www.wikidot.com/xml-rpc-api.php", false, $context);                     
    $response = xmlrpc_decode($file);                                         
    if ($response && xmlrpc_is_fault($response))                              
        {                                                                 
        print("Could not get ".$page);                                                  
        trigger_error("Wikidot XML-RPC Error:                             
        $response[faultString] ($response[faultCode])");
        Print("<hr>");
        var_dump($response);    
        }                                                                 
    else                                                                      
        {                                                                 
        return ($response);                                               
        }                                                                 
    }                                                                         
//----------------------------------------------------------------------------------------
 
?>

You can see a live demo of this on this page:page1

Related Files

File nameFile typeSize
content_array_api.phpPHP script3.58 kBInfo

Rate this Code

rating: +2+x

Discuss this Code

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License