Converting a code from bash to php -


i have existing code in bash greps keyword config file:

    [user1]     usrcid = 5654654654     usrsid = xdfdfsas22     usrmid = companyname1     usrsrt = secret1     urlenc = http://www.url1.com      [user2]     usrcid = 5654654667     usrsid = xdfdfsas45     usrmid = companyname2     usrsrt = secret2     urlenc = http://www.url2.com 

i store variable , use processing rest of script. want achieve convert behavior bash php , curl:

    f1=/etc/config/file.txt     cid=`grep "\[user1\]" -a 5 $f1 | grep usrcid | awk {'print$3'}`     sid=`grep "\[user1\]" -a 5 $f1 | grep usrsid | awk {'print$3'}`     mid=`grep "\[user1\]" -a 5 $f1 | grep usrmid | awk {'print$3'}`     srt=`grep "\[user1\]" -a 5 $f1 | grep usrsrt | awk {'print$3'}`     uri=`grep "\[user1\]" -a 5 $f1 | grep urlenc | awk {'print$3'}`     echo $cid $sid $mid $srt $uri 

i'm not php guru please excuse code below general perspective, below code understanding of want achieve:

    <?php     include "/etc/config/file.txt"      // *** equivalent code grep? ***      function get_data($url)     {      $ch = curl_init();          $timeout = 5;          curl_setopt($ch,curlopt_url,$url);          curl_setopt($ch,curlopt_returntransfer,1);          curl_setopt($ch,curlopt_connecttimeout,$timeout);          $data = curl_exec($ch);          curl_close($ch);          return $data;     }      // *** i'm not sure if 1 correct? ***  $returned_content = get_data('$uri/cid=$cid&sid=$sid&mid=$mid&srt=$srt')     echo $returned_content;     ?> 

this first time ask in stackoverflow thank in advance!

include doesn't think it's doing. won't variables set in text-file. if php code in file included, evaluate that, in case, it's text. see manual

what need use parse_ini_file() function. takes config file first argument, , boolean flag second. second argument used let function know should use sections in config file, do.

example:

file.txt:

[user1] usrcid = 5654654654 usrsid = xdfdfsas22 usrmid = companyname1 usrsrt = secret1 urlenc = http://www.url1.com  [user2] usrcid = 5654654667 usrsid = xdfdfsas45 usrmid = companyname2 usrsrt = secret2 urlenc = http://www.url2.com 

test.php:

<?php  $config = parse_ini_file("file.txt", true);  print_r($config);  ?> 

(see manual parse_ini_file())

this load config file $config variable, , contain following:

array (     [user1] => array         (             [usrcid] => 5654654654             [usrsid] => xdfdfsas22             [usrmid] => companyname1             [usrsrt] => secret1             [urlenc] => http://www.url1.com         )      [user2] => array         (             [usrcid] => 5654654667             [usrsid] => xdfdfsas45             [usrmid] => companyname2             [usrsrt] => secret2             [urlenc] => http://www.url2.com         )  ) 

now, construct url use:

$url = "{$config['user1']['urlenc']}/cid={$config['user1']['usrcid']}&sid={$config['user1']['usrsid']}&mid={$config['user1']['usrmid']}&srt={$config['user1']['usrsrt']}"; 

or construct dynamic way of iterating through array given in $config variable, account several sections. url can run through curl function got.


Comments

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -