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 6119539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:33:54+00:00 2026-05-23T15:33:54+00:00

I am not sure if this is even possible since I m new to

  • 0

I am not sure if this is even possible since I m new to PHP. I have a URL (for instance: http://www.mysite.com/data/response/) which gives me some cookie data as json respose ( {“cookie_name”:”value”,”ttl”:60} ). I have a php script that thows some output based on a cookie value. So, right on top of this php script I need to make a call to the above url, get the json response/interpret it and set a cookie based on the response. Can someone please help me with this?

Many thanks in advance.
L

  • 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-23T15:33:54+00:00Added an answer on May 23, 2026 at 3:33 pm

    Here it is:

    $url = 'http://www.mysite.com/data/response/';
    $result = file_get_contents($url);
    $json = json_decode($result, true);
    
    set_cookie('cookie_name', $json['value'], time() + ((int) $json['ttl']));
    

    As cwallenpoole mentioned in comments below, make sure that allow_url_fopen runtime configuration variable is set to TRUE (it is by default). If not – use ini_set('allow_url_fopen', 1), but it may be restricted by safe mode I think 🙂

    json_decode function has been introduced in PHP 5.2.0. If you have PHP >= 5.2 and json_decode is not available, please check if JSON extension is enabled (extension=json.so in your php.ini) and if you PHP was combiled with --disable-json flag.

    If you use pre-5.2 PHP then you can use following code (introduced by Anonymous user on PHP.net):

    if ( !function_exists('json_decode') ){
    function json_decode($json)
    {
    $comment = false;
    $out = '$x=';
    
    for ($i=0; $i<strlen($json); $i++)
    {
        if (!$comment)
        {
            if (($json[$i] == '{') || ($json[$i] == '['))       $out .= ' array(';
            else if (($json[$i] == '}') || ($json[$i] == ']'))   $out .= ')';
            else if ($json[$i] == ':')    $out .= '=>';
            else                         $out .= $json[$i];          
        }
        else $out .= $json[$i];
        if ($json[$i] == '"' && $json[($i-1)]!="\\")    $comment = !$comment;
    }
    eval($out . ';');
    return $x;
    }
    }
    

    If allow_url_fopen is set to 0 and you cannot change it by ini_set or by setting it in your php.ini file, that you can stick to cURL (if cURL extension is enabled :)):

    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER_HEADER, true);
    
    $result = curl_exec($ch);
    curl_close($ch);
    

    The rest code should work as in first example. Hope it helps!

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

Sidebar

Related Questions

I'm not sure this is even possible but I know if it is, the
First of all I'm not sure this is even possible, however I need to
I'm not sure if this is even possible, but is there a way to
Ok, not sure if this is even possible, but I want to change the
Hello! I'm not even sure if this is possible, but hopefully it is in
I'm very new to threading and thus am not quite sure if this even
I am not sure this is possible. I have a bunch of @Helper 's
I'm not entirely sure if this is even possible, but I'll try my best
I'm not sure if this is even what you call it, but basically, I
I'm not even sure what this principle is called or how to search for

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.