Category Archives: PHP - Page 4

Get PUT and DELETE data

REST is very popular as a web service technique and PHP (v5.1) has got great support for POST through the $_POST (and $_GET for that matter) but for PUT, DELETE and OPTIONS we have to do a little different.

To get the data from a PUT request we can use this technique:

if($_SERVER['REQUEST_METHOD'] == 'PUT') {  
    $data = file_get_contents('php://input');  
} 

This will put the data from the PUT request into the $data variable. If you want DELETE och OPTION just check for those request methods instead and use the same fetch method as above.

Syntax check for PHP

Sometimes you just want to check the syntax in a PHP file without running it. For this the good people developing PHP made the -l option. To use:

php -l <filename>

This will check the syntax of <filename> file. If there are no errors you will get: No syntax errors detected in <filename>

Tested with PHP v5.1.6

Create individual xml files from one file with many xmls

Say you have a file with a bunch of nice-xml documents inside and you want them all as individual files. I come across this problem quite often so I thought it would be a good idea to have a script here showing how I usually sole this problem (PHP >= 4.3).

<?php
//Check the number of arguments
if($argc != 5){
    echo "Usage: splitxml <filename> <starttag> <endtag> <path>n";
    echo "<filename> - filename of file with xmls inn";
    echo "<starttag> - first tag of the xml you want to extractn";
    echo "<endtag> - last tag of the xml you want to extractn";
    echo "<path> - where you want the files to be created";
    exit();
}

// Read file into an array
$xml = file($argv[1]);

$starttag = $argv[2];
$endtag = $argv[3];
$resultpath = $argv[4];

$tmp = array();
$counter = 0;
$recording = false;

foreach($xml as $row){
    // Found start tag - start "recording"
    if(strpos($row, $starttag) !== false){
        $recording = true;
    }
    // Save all rows if "recording"
    if($recording){
        $tmp[] = $row;
    }
    // Found end tag - stop "recording" and save file
    if(strpos($row, $endtag) !== false){
        $fp = fopen($resultpath.'/file'.$counter++.'.xml', 'w');
        fwrite($fp, implode("",$tmp));
        fclose($fp);
        $tmp = array();
        $recording = false;
    }
}
?>

Here is an example of how the file can look (books.xml):

--------------------
<book>
    <title>Ronja</title>
    <author>Astrid Lindgren</author>
</book>
--------------------
<book>
    <title>Barnen i Bullerbyn</title>
    <author>Astrid Lindgren</author>
</book>
--------------------

To get these book xml into separate files just run the above script with the following parameters:

php splitxml books.xml '' '' books/

This will create the files file0.xml and file1.xml in the directory books/

file0.xml:

<book>
    <title>Ronja</title>
    <author>Astrid Lindgren</author>
</book>

file1.xml:

<book>
    <title>Barnen i Bullerbyn</title>
    <author>Astrid Lindgren</author>
</book>