Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7653199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:58:11+00:00 2026-05-31T11:58:11+00:00

I currently have an existing code in bash that greps a keyword from a

  • 0

I currently have an existing code in bash that greps a keyword from a 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 it as a variable and use it for processing the rest of the script. What I want to achieve is to convert the behavior from bash to php and do a 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 really not a php guru so please excuse the code below but from a general perspective, the below code is my understanding of what I want to achieve:

    <?php
    include "/etc/config/file.txt"

    // *** the 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 this one is correct? ***

$returned_content = get_data('$URI/cid=$CID&sid=$SID&mid=$MID&srt=$SRT')
    echo $returned_content;
    ?>

This is my first time to ask in stackoverflow so I would like to thank you in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:58:13+00:00Added an answer on May 31, 2026 at 11:58 am

    Include doesn’t do what you think it’s doing. It won’t get the variables you set in the text-file. If it were PHP code in the file you included, it would evaluate that, but in this case, it’s only text. See the Manual

    What you need is to use the parse_ini_file() function. It takes the config file as first argument, and a boolean flag as the second. The second argument is used to let the function know that you should use sections in your config file, which you 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 the manual for parse_ini_file())

    This will load the config file to the $config variable, and it will contain the 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, to construct an URL you could use:

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

    Or construct a dynamic way of iterating through the array given in the $config variable, to account for several sections. This URL you can run through the cURL function you got.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have an existing C# .NET code base that is currently coded to directly access
I currently have existing code that automates and email and sends files. I now
I am working on splitting out an existing, working application that I currently have
I have an existing system built in Java we currently expand that by adding
We currently have a 10 year old nasty, spaghetti-code-style SQL Server database that we
I currently have an application that calls creates and displays charts from various objects'
I have: existing object oriented native code API (non GUI) GUI application that works
We currently have a quote page which lists all existing quotes that we would
I have an application that is currently creating a text file to import into
We currently have a 10 year old nasty, spaghetti-code-style SQL Server database that we

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.