1

I've got a url that returns JSON style location data for a user. I'd like to set this data into cookies but I don't know how. I'm a very inexperienced user who's just learning and would really appreciate some help with this. Here is an example of the url:

http://beta.geocoding.cloudmade.com/v3/8ee2a50541944fb9bcedded5165f09d9/api/geo.location.search.2?format=json&source=OSM&enc=UTF-8&limit=10&q=48.77615073;9.16416465

Thanks so much!

Zonamaster
  • 43
  • 8
  • This link should be helpful: http://www.w3schools.com/js/js_cookies.asp – Jerzy Pawlikowski Dec 09 '13 at 22:47
  • @barjey: I think that OP doesn't need JS solution. [And don't forward users to w3schools](http://www.w3fools.com/). – Glavić Dec 09 '13 at 22:58
  • Welcome to stackoverflow! What have you tried so far? Show us some code please. – user1251007 Dec 09 '13 at 23:11
  • @Glavić, didn't know that about w3schools, thanks for the info. – Zonamaster Dec 09 '13 at 23:16
  • @barjey, I think I need go with php on this one since it interacts better with my cms. – Zonamaster Dec 09 '13 at 23:17
  • @user1251007 Actually, the only code I have is in javascript. I've been banging my head on that for a day and then it dawned on me that even if i got that to work, for me to get the data into my cmd (expressionengine) i'd need to use php anyway. I do know how to set cookies in php but parsing a json response into cookies is a concept I don't grasp enough to even sketch out some example code. – Zonamaster Dec 09 '13 at 23:39

1 Answers1

0

Just an untested idea ...

  1. Read the json file from the URL as simple text string.

    $json_url = 'http://beta.geocoding.cloudmade.com/v3/8ee2a50541944fb9bcedded5165f09d9/api/geo.location.search.2?format=json&source=OSM&enc=UTF-8&limit=10&q=48.77615073;9.16416465';
    $text = file_get_contents($json_url);
    
  2. Store as json and use base64, just in case:

    $value = base64_encode(json_encode($text));
    
  3. Store the cookie:

    setcookie("test", $value);
    
user1251007
  • 15,891
  • 14
  • 50
  • 76
  • 1
    That worked very well, the json data slips right into a cookie, just perfect! There is another aspect to this that I didn't realize would be a problem till now. I need to supply the lat/long of the user in the url of the json request. As far as I know that can only be obtained by javascript. Do you have any advice how to construct the url so I can include lat/long data from javascript? Or, is there way to get lat/long from php? I've looked everywhere for this and can't find any way to do this... – Zonamaster Dec 10 '13 at 01:59
  • Just a quick search yielded [this](http://stackoverflow.com/questions/9789283/how-to-get-javascript-variable-value-in-php). Please also consider upvoting my answer. ["If an answer has helped you solve your problem and you accept it you should also consider voting it up."](http://meta.stackexchange.com/a/5235/181353) – user1251007 Dec 10 '13 at 09:27
  • 1
    I'd love to upvote you as you really helped me a lot but I can't do it because apparently I'm not allowed to up vote until I have a reputation over 15. I'm 1/2 way there! When I get there (assuming I do) I'll gladly come back here and do it! – Zonamaster Dec 10 '13 at 17:31
  • BTW, the tip for passing the variables was great. I'm probably going to play with this all day to put it together. Thanks! – Zonamaster Dec 10 '13 at 17:32