Friday, June 28, 2013

How to handel warning error using exceotion in php

<?php
function custom_error($errno, $errstr, $errfile, $errline){
echo "Xml url not valid $errfile";
die();
}
function getData() {
 if(!set_error_handler('custom_error')){
    echo "Error :";

    }

    try {
        $xml = new SimpleXMLElement('http://publisher.usb.api.shopping.com/publisher/3.0/reasdfst/GeneralSearch?apiKey=752d3653-2797-4544-9ca1-asdf&trackingId=sadf&numItems=1&&keyword=strollers', null, true);
    }catch(Exception $e) {
        restore_error_handler();

    }

    return $xml;
}

    $xml = getData();


?>

Tuesday, June 25, 2013

download and save image into server base64 decode using php

 define('UPLOAD_DIR', 'C:\inetpub\wwwroot\PHP\images\Shareimage/');
 $imagePath=$_POST['img_val'];


       /* header("Content-Transfer-Encoding: binary");
        header("Content-Type: image/jpg");
        header("Content-Disposition: attachment; filename=custom-img.jpg");
        //start feeding with the file
readfile($imagePath);
*/



 
$imagePath = str_replace('data:image/jpeg;base64,', '', $imagePath);
$imagePath = str_replace(' ', '+', $imagePath);
$data = base64_decode($imagePath);
$file = UPLOAD_DIR  .'customshare.jpeg';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';