fetch array from a given url in php - php

i have a url.. eg http://www.example.com/index.php?rou=feed/web_api/categories&key=4&parent=0&level=10
when i hit the link simply on browser it displays a long list of multidimensional array.
I want to know that is it possible to store the url in a variable, something like this
$link=http://www.example.com/index.php?rou=feed/web_api/categories&key=4&parent=0&level=10
And then display the array in it as
echo '<pre>';
print_r($link);
echo '</pre>';
and then fetch the values from the array
I tried doing so but it displayed only a blank screen. Not sure if i am going on the right track. can anyone guide me

I think you are looking for one of these:
$data = file_get_contents('http://example.com/data.php');
This will fetch the raw data from the specified URL, and store the contents to the variable.
You may then need to implement your own code to actually parse the raw data, depending on your encoding type.

Related

Display a data from an array

I just want to ask how to display the data from an array like this? Here's the link
This is my first time seeing an array like this long and I am confused. I tried getting the data from the cart_message and I get an array as a result. I'm sure this is the wrong format to get it. I am a newbie when it comes to this.
$messages['cart_message'];
Update:
Here's what I am trying to do.
I am trying to display the string on the cart_message
I want to display the text on the cart_message on the side cart template. So, to do that, I thought it will display if I use this filter
$yith_message = apply_filters( 'yith_ywpar_panel_messages_options', $messages);
echo '<pre>'; var_dump($yith_message); echo '</pre>';
The result is Null
Try:
var_export($messages);
More info: var_export

Retrieve string from json

I'm trying to retrieve a URL string from some json code.
Here is the json code
{"files":["www.example1.com"],"previews":["www.example1preview.com"],"meta":{},"userId":"guest","product":{"id":"2335","name":"standard"},"type":"u"}
Looking at what I've seen in the PHP manual I'm trying to retrieve previews like this.
<?php
ob_start();
include('getjson.php');
$meta_value_json = ob_get_clean();
echo $meta_value_json;
$meta_value_json = json_decode($meta_value_json);
print $meta_value_json->{'previews'};
?>
This doesn't seem to output the value however.
By experimenting with php -a command on terminal, I've put your json into json_decode and managed to get your link by just doing:
print $meta_value_json->previews[0];
The only reason to use print $meta_value_json->{'previews'}; at least according to php documentation is if you want an object as output and the key trying to retrieve is numerical or of a type that is not supported by php.
By experimenting a bit further, the reason that print $meta_value_json->{'previews'}; fails is because print expects a string, in our case here previews is an array. Therefore if you do print $meta_value_json->{'previews'}[0]; it will also work as expected.
You need to get the value from your decoded json like this: $class->parameter.
Knowing that previews is an array, you will also need to choose a specific element from it to print ( I got the first one ):
<?php
ob_start();
include('getjson.php');
$meta_value_json = ob_get_clean();
echo $meta_value_json;
$meta_value_json = json_decode($meta_value_json);
print $meta_value_json->previews[0]; /// get the specific value
?>

Grab, filter and show this data in PHP

This is example what I mean:
I wanna grab result from this url web1.com/do.php?id=45944
Example output:
"pk":"bn564vc3b5yvct5byvc45bv","1b":129,"isvalid":true,"referrer":true,"mobile":true
Then, I will show data result on other site web2.com/show.php
But I just wanna see data value "pk", "1b" and "isvalid". I don't need "referrer" and "mobile" data.
So, when I access web2.com/show.php, it just show data like this:
bn564vc3b5yvct5byvc45bv 129 true
file_get_contents web1.com/do.php?id=45944
Grab this result "pk":"bn564vc3b5yvct5byvc45bv","1b":129,"isvalid":true,"referrer":true,"mobile":true
Filter and show value "pk", "1b" and "isvalid" on web2.com/show.php
So, can you help me with simple php code/script?
Sorry if you don't understand because my english.
Is this source providing JSON-formatted data? That is, with { }'s around it?
If so, use PHP's built in json_decode function. (Man page at http://php.net/manual/en/function.json-decode.php) It will parse the JSON data for you and return an associative array. For example
$your_JSON_data = '{"pk":"bn564vc3b5yvct5byvc45bv","1b":129,"isvalid":true,"referrer":true,"mobile":true}';
$your_array = json_decode($your_JSON_data);
echo $your_array['pk'] . ' ';
echo $your_array['1b'] . ' ';
echo $your_array['isvalid'];
If, for some reason, there are no JSON-style curly braces around your data, you can append them.... for example,
$proper_JSON = "{$bad_json_data}";
however, if that's the case, I'd wonder why it wasn't properly formatted in the first place. In that case it may be safer to use the PHP explode function (http://php.net/manual/en/function.explode.php)

I'm fairly new to JSON, and I'm trying to get the user data from google + JSOn Code is

I'm fairly new to JSON, and I'm trying to get the user data from google +
JSOn Code is
https://www.googleapis.com/plus/v1/people?query=saurabh+sharma&key=AIzaSyADJjj8IeKuGb-woleHKTVouSlvAJUpTrs
Please help me to retrive the user profile.. in php
var_dump(json_decode(file_get_contents('https://www.googleapis.com/plus/v1/people?query=saurabh+sharma&key=AIzaSyADJjj8IeKuGb-woleHKTVouSlvAJUpTrs')));
It's indeed a bit of a mixed potato-puree (=confusion) between arrays and objects. That JSON is entirely object, except for the array 'items', which is an array of... objects.
Try this:
<?php
$strUrl = "https://www.googleapis.com/plus/v1/people?query=saurabh+sharma&key=AIzaSyADJjj8IeKuGb-woleHKTVouSlvAJUpTrs";
$strContents = file_get_contents($strUrl);
$objPeopleFeed = json_decode($strContents);
//It's an array of objects, so:
echo "<h1>{$objPeopleFeed->title}</h1>";
foreach($objPeopleFeed->items as $objUser)
{
echo "
<p>
<img src='{$objUser->image->url}' />
<a href='{$objUser->url}'>{$objUser->displayName}</a>
<i>{$objUser->objectType}</i>
</p>";
}
?>
What it does: It gets the contents from the web (which is JSON), interpretes it as JSON into valid PHP structures. From the structure, we print the title as a H1 header. From the items, which is an array, we loop through each one, print the image src from $objUser->image->url, print the user link $objUser->url with its name $objUser->displayName, and optionally its type of object $objUser->objectType that is registered on Google.
Because everything is kind of objects, you use the object to variable syntax '->xyz' instead of array indexes '["xyz"]', and I guess you got stuck there. The $objPeopleFeed->items is a non-associative array, so you use numbers to loop through the items ($objPeopleFeed->items[0] for the first item, 1 for the second, etc...). As a final cookie to you, you can use count($objPeopleFeed->items) as a results count.

Cake PHP Link Doesn't Display

I have string like "Venditoris: Beware of Scams » Blog Archive » Trilegiant Complaints ..." in Database but when I try to display it ,It is not displaying.
So,I used html_entity_decode function but still it is not display.
I am Using cakePHP.below is my code to display that links.
echo $html->link(html_entity_decode(
$listing_end_arr[$i]['Listing']['listing_title'],ENT_QUOTES),
$listing_end_arr[$i]['Listing']['listing_url'],
array('target'=>'_blank', 'style'=>'color:'
. $colorArr[$listing_end_arr[$i]['Listing']['listing_sentiment']])) ;
Please Help me.
Check the CakePHP manual if you are using $html->link correctly. If so, var_dump the return value instead of echoing it. If it is empty, do
var_dump( $listing_end_arr[$i]['Listing'] );
to see what the Listing key contains. If the desired content is not in the dump, you know the error is elsewhere; probably in fetching the string from where it is stored.
Also, instead of using array[n][foo][bar][baz], consider assigning the subarray to a variable while looping over the array, e.g. $listing = array[n][foo][bar], so you can just do $listing[baz]. This will dramatically increase readability of your code.
inspect generated html first.. your code should echo a link, maybe it's just not visible (styling, color..).

Categories