JScrambler API Usage Tutorial in PHP
Two basic usage cases of the API are presented here. Use any to get familiar with the API.
Upload a JavaScript file to minify or obfuscate
To minify or obfuscate a JavaScript using the API you may use the following php code. Download code here.
/**
* Simple post example - upload a JavaScript file to minify or obfuscate
*/
require_once 'jscrambler.php';
$access_key = "YOUR_ACCESS_KEY"; // edit
$secret_key = "YOUR_SECRET_KEY"; // edit
$path_to_js = "PATH_TO_YOUR_JS"; // edit
$jscrambler = new Jscrambler($access_key, $secret_key, 'api.jscrambler.com', '80');
$parameters = array('file_path' => $path_to_js,
// add more transformations
'rename_all' => 'varname|varname2|varname3',
'whitespace' => '%DEFAULT%');
// post
$result = $jscrambler->post('/code.json', $parameters);
$content = $result["transfer"];
// print
echo $content;
Edit YOUR_ACCESS_KEY, YOUR_SECRET_KEY and the PATH_TO_YOUR_JS and run the following on the command line:
:~ php post.php
Example Response
{
"id":"401c600215aab40ea4709a3a0075ef196000cdf0",
"received_at":"2011-01-10 08:41:51",
"filename":"example.js",
"size":16868
}
The id on the json response may be used to request (GET) the resulting minified or obfuscated JavaScript from the API.
Download your minified or obfuscated JavaScript
To download the minified or obfuscated JavaScript from the API you may use the following php code. Download code here.
/**
* Simple get example - get your minified or obfuscated JavaScript
*/
require_once 'jscrambler.php';
$access_key = "YOUR_ACCESS_KEY"; // edit
$secret_key = "YOUR_SECRET_KEY"; // edit
if($argc < 2){
echo "Try: php getbyid.php _id_\n";
echo "Example: php getbyid.php 9b44095ef94b7300000009c69fc206906aa9d231\n";
die;
}
$jscrambler = new Jscrambler($access_key, $secret_key, 'api.jscrambler.com', '80');
// get
$result = $jscrambler->get("/code/{$argv[1]}.json");
$content = $result["transfer"];
// print
echo $content;
Edit YOUR_ACCESS_KEY and YOUR_SECRET_KEY. Finally run the following on the command line:
:~ php getbyid.php _id_
Example Response
{
"id":"401c600215aab40ea4709a3a0075ef196000cdf0",
"received_at":"2011-01-10 08:41:51",
"filename":"example.js",
"size":"16868",
"techniques":["whitespace","rename_local"]},
"finished_at":"2011-01-10 08:41:57",
"new_filename":"example.jscrambler.js",
"new_size":"5577",
"code":"..."
}
The json message contains the JavaScript as the value of the code key.
To get the JavaScript code from the json message (decode) use the following:
...
$arr_json = json_decode($content, true);
$js_code = $arr_json['code'];
// print
echo $js_code;
Requirements
- PHP 5.2.x or higher (http://php.net/downloads.php)
- libcurl (http://pt.php.net/manual/en/curl.requirements.php)
Back ↵