I want to fetch the value of until from following url,
https://graph.facebook.com/v2.1/168820059842589/feed?fields=id,from,message,comments%7Bfrom,id,created_time,message,attachment,comments%7Bfrom,id,created_time,message,attachment%7D%7D,type,created_time,updated_time,picture,name,caption,link,source,description,likes,story,place,story_tags&limit=250&include_hidden=true&until=1394190620&__paging_token=enc_AeyIp_kAzWE_u3avDbSjlT69EtTXCEOSDLyp0xSOTptJHfmlHKARfuTm197SkZAPDhKsXAtfu9G7jqzn75ymXK60UwG3nR2itFLk-IhY_hdOzQ
Thanks
You can use parse_str():
$url = 'https://graph.facebook.com/v2.1/168820059842589/feed?fields=id,from,message,comments%7Bfrom,id,created_time,message,attachment,comments%7Bfrom,id,created_time,message,attachment%7D%7D,type,created_time,updated_time,picture,name,caption,link,source,description,likes,story,place,story_tags&limit=250&include_hidden=true&until=1394190620&__paging_token=enc_AeyIp_kAzWE_u3avDbSjlT69EtTXCEOSDLyp0xSOTptJHfmlHKARfuTm197SkZAPDhKsXAtfu9G7jqzn75ymXK60UwG3nR2itFLk-IhY_hdOzQ';
$arr = array();
parse_str($url,$arr);
echo $arr['until'];
//outputs 1394190620
Related
I have the url http://pubapi.cryptsy.com/api.php?method=singleorderdata&marketid=132 which leads to an array.
I want to get the value of the first 'sellorders' which in this case is: 0.00000048 and store it in the variable $sellorderprice.
Can anyone help?
Thanks.
Just access the url contents thru file_get_contents. Your page actually return a JSON string, to get those values into meaningful data, decode it thru json_decode, after that access the data needed accordingly:
$url = 'http://pubapi.cryptsy.com/api.php?method=singleorderdata&marketid=132';
$data = json_decode(file_get_contents($url), true);
$sellorderprice = $data['return']['DOGE']['sellorders'][0]['price'];
echo $sellorderprice;
That code actually points directly to index zero 0 which gets the first price. If you need to get all items an just simply echo them all you need to iterate all items thru foreach:
foreach($data['return']['DOGE']['sellorders'] as $sellorders) {
echo $sellorders['price'], '<br/>';
}
Its simple, you just have to decode json like this:
$json = file_get_contents("http://pubapi.cryptsy.com/api.php?method=singleorderdata&marketid=132");
$arr = json_decode($json, true);
$sellorderprice = $arr['return']['DOGE']['sellorders'][0]['price'];
When I use var_dump($_POST), I get this:
array(2) {
["param"]=>
string(327) "{"username":"asd","password":"asdasd","language":"7"}"
}
I need to get username. I've tried it this:
$arr = json_decode($_POST['param'], true);
echo $arr["username"];
But it doesn't work.
Anyone know where my error is and what the right way to get element username is?
Please check the edited answer:
$arr = json_decode($_POST['param'],true);
echo $arr['username'];
Your jason encoded string seems to be inside the param element, Try this:
$arr = json_decode($par['param'], true);
echo $arr["username"];
I am trying to pass an array in a url so I tried this:
?question_id=10&value=1&array=["Saab","Volvo","BMW"]
This didn't work (didn't think it would, but it's a start).
It's a key and value array anyway so I needed something like this:
&array[28]=1&array[9]=1&array[2]=0&array[28]=0
But that didn't work either
in jquery try this
var arr = [1, 4, 9];
var url = '/page.php?arr=' + JSON.stringify(arr);
window.location.href = url;
If you want to pass an array, you just have to set the different parts of the array in the URI like
http://example.com/myFile.php?cars[]=Saab&cars[]=volvo&cars[]=BMW
So you get your formated array in $_GET['cars']
print_r($_GET['cars']); // array('Saab', 'Volvo', 'BMW')
To get such a result in javascript you can use this kind of code
var cars = ['Saab', 'Volvo', 'BMW'],
result = '';
for (var i = cars.length-1; i >= 0; i--) {
results += 'cars[]='+arr[i]+'&';
}
results = results.substr(0, results.length-1); // cars[]=BMW&cars[]=Volvo&cars[]=Saab
Try to pass like this
?question_id=10&value=1&my_array=Saab,Volvo,BMW
and you can get like
<?php
$my_array = explode(',',$_GET['my_array']);
?>
or you can try like this
$aValues = array('Saab','Volvo','BMW');
$url = 'http://example.com/index.php?';
$url .= 'aValues[]=' . implode('&aValues[]=', array_map('urlencode', $aValues));
You may also try this:
?arr[]="Saab"&arr[]="BMW"
Using the jQuery getUrlParam extension I can get url variables very easily
or in PHP
var_dump($_GET);
This snippet works fine:
$url="Http://www.youtube.com/watch?v=upenR6n7xWY&feature=BFa&list=PL88ACC6CB00DC2B44&index=4";
$parsed_url=parse_url($url);
echo "<br><br><pre>";
print_r(parse_url($url));
echo "</pre>";
echo $parsed_url['query'];
But when I add the below:
echo "<br><br><pre>";
$parsed_str=parse_str($parsed_url['query']);
print_r($parsed_str);
echo "</pre>";
Nothing happens. I suspect parse_str() isn't working as it should. Any ideas what I might be doing wrong?
If you want to have the result of parse_str() in an array, pass the array as the second argument:
parse_str($parsed_url['query'], $parsed_str);
var_dump($parsed_str);
I'm going to assume that the user inputs the URL. If so, do not use parse_str without a second argument!. Doing so would result in a security risk where the user can overwrite arbitrary variables with the value of their choice.
parse_str() doesn't return anything. It populates variables.
For example, if you have a query string of $query = "param=1&test=2"
after
parse_str($query);
you can check var_dump($param) and var_dump($test) - those two variables would be created for you.
Basically, parse_str converts
v=upenR6n7xWY&feature=BFa&list=PL88ACC6CB00DC2B44&index=4
To
$v = "upenR6n7xWY"
$feature = "BFa"
$list = "PL88ACC6CB00DC2B44"
$index = 4
If I have a url that looks like this, what's the best way to read the value
http://www.domain.com/compute?value=2838
I tried parse_url() but it gives me value=2838 not 2838
Edit: please note I'm talking about a string, not an actual url. I have the url stored in a string.
You can use parse_url and then parse_str on the query.
<?php
$url = "http://www.domain.com/compute?value=2838";
$query = parse_url($url, PHP_URL_QUERY);
$vars = array();
parse_str($query, $vars);
print_r($vars);
?>
Prints:
Array
(
[value] => 2838
)
For http://www.domain.com/compute?value=2838 you would use $_GET['value'] to return 2838
$uri = parse_url($uri);
parse_str($uri['query'], $params = array());
Be careful if you use parse_str() without a second parameter. This may overwrite variables in your script!
parse_str($url) will give you $value = 2838.
See http://php.net/manual/en/function.parse-str.php
You should use GET method e.g
echo "value = ".$_GET['value'];