Whether I rewrote the bash script in PHP?

Was this a script that verifies all files on my website and on the other, he looked like on the date of the file you downloaded and the date from another site if the files were the same (or date of the file you downloaded more than the file date from another site) that the script didn't shake, the headlines were determined. If the files were different then rocked. He worked on the bash (sh script) before, now I want to translate to php.

The script downloaded file.json.gz c another site and unpacked already in my folder with the website
#!/bin/bash
FNAME="file.json"
URL="http://remotesite.ru/fff.json"
GNAME="/home/user/bin/getjson/${FNAME}.gz"
curl --fail-R -z "${GNAME}" -o "${GNAME}" -H "Accept-Encoding: gzip" "${URL}" && \
 gunzip -c "${GNAME}" > /home/user/mysite.EN/files/"${FNAME}"


Here is the changed script in PHP:

<?php

getJsonPooling function($localFile,$arr,$num,$remoteFile){
$numReq = $num+1;
echo 'CONNECTION ATTEMPT: '.$numReq.'<br-->';
echo 'Connect to: '.$arr[$num].'<br>';
$getJson = new getJson();
$gg= $getJson->get($localFile, $arr[$num] ,$remoteFile);
if ( ($gg['returnurl']['errno']) || 
 ($gg['returnfile']['errno']) && 
 ($num <= count($arr)-2 ) ) {
$num++;
getJsonPooling($localFile,$arr,$num,$remoteFile);
}
var_dump($gg);
}

class getJson{
 static public function get($localFile,$url,$remoteFile)
{


$url .=$remoteFile;

if (file_exists($localFile)) {
$timestamp = filemtime($localFile);
}

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,$url); //conf

/*curl_setopt($ch, CURLOPT_PROXY, '51.255.138.248:3128'); //PROXY's*/

curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return what is returned to the server
curl_setopt($ch, CURLOPT_HEADER, true); // read the header
curl_setopt($ch, CURLOPT_NOBODY, 1); // read the header ONLY without the body
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); // connection timeout
curl_setopt($ch, CURLOPT_TIMEOUT, 60); // timeout response
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE); //no cache
curl_setopt($ch, CURLOPT_FILETIME, TRUE); //get timestamp of file
curl_setopt($ch, CURLOPT_ENCODING, "gzip"); //setting the encoding
curl_setopt($ch, CURLOPT_TIMEVALUE, $timestamp); //time of the local file for verification

curl_setopt($ch, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE); //If the file has not changed receive code 304

 curl_exec( $ch );
 $returnurl = curl_getinfo( $ch );
 $returnurl['errno'] = curl_errno( $ch );
 $returnurl['errmsg'] = curl_error( $ch );

if( 
 ($returnurl['http_code'] == '200') &&
 ($returnurl['errno'] == '0')
){

echo '<br> the File has changed, swing!';
echo '<hr>';
$ch2 = curl_copy_handle($ch); //Copy previous descriptor
curl_setopt($ch2, CURLOPT_HEADER, false); // read the header
curl_setopt($ch2, CURLOPT_NOBODY, false); // read the header ONLY without the body
$outData = curl_exec( $ch2 );
$isJSON = (is_string($outData) &&
(is_object(json_decode($outData)) ||
is_array(json_decode($outData)))) ? true : false;

 $returnfile = curl_getinfo( $ch2 );
 $returnfile['errno'] = curl_errno( $ch2 );
 $returnfile['errmsg'] = curl_error( $ch2 );
 $returnfile['valid'] = $isJSON;

curl_close($ch2);

if ($returnfile['http_code'] != '200') {
 echo 'Error on the server when receiving file: '.$returnfile['http_code'];
}

if ($returnfile['errno'] != '0'){
echo '<br> Error getting file: '.$returnfile['errno'].'<br>';
echo '<br>'.$returnfile['errmsg'].'<br>';
}

if($isJSON) {
echo '<br>the Received file is correctly written! <br/>';
file_put_contents($localFile, $outData);
$GLOBALS['data'] = $outData;
unset($outData);
} else {
echo '<br>the resulting file is NOT correct <br>';
die();
}

} else if ($returnurl['http_code'] == '304'){
echo '<br> the File has not changed <br>';
} else if ($returnurl['errno'] != '0'){
echo '<br> Error: '.$returnurl['errno'].'<br>';
echo '<br>'.$returnurl['errmsg'].'<br>';
}

curl_close($ch);

$return['returnurl'] = $returnurl;
$return['returnfile'] = $returnfile;

 return $return;
}
}
?>


Run as so:

$localFile = './files/local.json';
$remoteFile = 'remotefile.json';

//Mirror
$getJsonLinks = array(
'http://serv1.ru/',
'http://serv2.com/',
'http://serv3.org/'
);

//local file, an array with links to the website, the reference number from array (if the link is not working go to the following link), the file to download the link
getJsonPooling($localFile,$getJsonLinks,0,$remoteFile);

if($data){
$data = json_decode($data, true);
} else {
die('<br> $data not received die(); <br>');
}


It seems to work )
What can I tweak for optimization/simplicity, or is it all in a few lines written in General ?
June 5th 19 at 23:01
1 answer
June 5th 19 at 23:03
1. What you don't like bash?
2. Bash nothing is checked, just rocked korlam.
1. As soon as the file is downloaded, I $data then running a php script, so convenient.
2. Well I have not checked if this bash was at my crown and I received reports in the mail. there it was visible when swung in a file and when not to. - Telly_Davis65 commented on June 5th 19 at 23:06
Was wrong. Flag -z
But, you can still pull 100% working bash, and not steamed =) - albertha.Medhur commented on June 5th 19 at 23:09
Type right ?)
#!/bin/bash
FNAME="file.json"
URL="http://remotesite.ru/fff.json"
GNAME="/home/user/bin/getjson/${FNAME}.gz"
curl --fail-R -z "${GNAME}" -o "${GNAME}" -H "Accept-Encoding: gzip" "${URL}" && \
 gunzip -c "${GNAME}" > /home/user/mysite.EN/files/"${FNAME}"
php /home/user/php/getjson/run.php

The php script will run immediately after unpacking ?
well, the best course on php to do, where I can check different to make the type isJSON and you never know which data will come, can do polling types with different links, etc ... easier, and with bash I have no friends - Telly_Davis65 commented on June 5th 19 at 23:12
,
Yes, you can from bash to pull a php script that will do all sorts of isJSON

Either of php, you can pull through phpexec only
curl --fail-R -z "${GNAME}" -o "${GNAME}" -H "Accept-Encoding: gzip" "${URL}" && \
gunzip -c "${GNAME}" > /home/user/mysite.EN/files/"${FNAME}" - Jeff28 commented on June 5th 19 at 23:15

Find more questions by tags PHPbash