how to get all data
i have some problem when i want to print my link in database and the format data in database is saved from json_encode() the data is ([{"link":"google.com"},{"link":"facebook.com"},{"link":"instagram.com"}]), and i want to print as google.com, facebook.com, instagram.com in my web, i use codeigniter framework
i have tried this
$link = json_decode($row['LINK'], true);
$link[0]["link"];
and the result just google.com
You can do this way to get the domain link,
<?php
$row = '[{"link":"google.com"},{"link":"facebook.com"},{"link":"instagram.com"}]';
$array = json_decode($row,true);
foreach($array as $key=>$value){
echo $value['link'].PHP_EOL;
}
?>
DEMO: https://3v4l.org/dElvA
OR array_colunn() to get all the domains in an array,
print_r(array_column($array,'link'));
Related
I have the URL https://android.rediptv2.com/ch.php?usercode=5266113827&pid=1&mac=02:00:00:00:00:00&sn=&customer=GOOGLE&lang=eng&cs=amlogic&check=3177926680
which outputs statistics.
For example:
[{"id":"2972","name":"MBC 1","link":"http://46.105.112.116/?watch=TR/mbc1-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC1En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc1.png"},{"id":"1858","name":"MBC 2","link":"http://46.105.112.116/?watch=TN/mbc2-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC2En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc2.png"},{"id":"1859","name":"MBC 3","link":"http://46.105.112.116/?watch=TN/mbc3-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc3.png"}]
I want to get the value of link count.
Can anyone help?
I tried to do:
<?php
$content = file_get_contents("https://android.rediptv2.com/ch.php?usercode=5266113827&pid=1&mac=02:00:00:00:00:00&sn=&customer=GOOGLE&lang=eng&cs=amlogic&check=3177926680");
$result = json_decode($content);
print_r( $result->link );
?>
But it didn't work.
Put the JSON in an editor and you'll see that it's an array and not an object with the link attribute. This is why you cannot access it directly. You have to loop over the items and then you'll be able to access the link property of one of the items. If you need to access the link by id, as you asked 4 months later, then just create a dictionnary in an array indexed by id and containing just the interesting data you need.
PHP code:
<?php
// The result of the request:
$content = <<<END_OF_STRING
[{"id":"2972","name":"MBC 1","link":"http://46.105.112.116/?watch=TR/mbc1-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC1En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc1.png"},{"id":"1858","name":"MBC 2","link":"http://46.105.112.116/?watch=TN/mbc2-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC2En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc2.png"},{"id":"1859","name":"MBC 3","link":"http://46.105.112.116/?watch=TN/mbc3-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc3.png"}]
END_OF_STRING;
$items = json_decode($content);
echo '$items = ' . var_export($items, true) . "\n\n";
// Create a dictionnary to store each link accessible by id.
$links_by_id = [];
// Loop over all items:
foreach ($items as $i => $item) {
// Show how to access the current link.
echo "Link $i = $item->link\n";
// Fill the dictionary.
$links_by_id[(int)$item->id] = $item->link;
}
// To access the first one:
echo "\nFirst link = " . $items[0]->link . "\n";
// Example of access by id:
// The id seems to be a string. It could probably be "1895" or "zhb34" or whatever.
// (If they are only numbers, we could convert the string to an integer).
$id = "1859";
echo "\nAccess with id $id = " . $links_by_id[$id] . "\n";
Test it here: https://onlinephp.io/c/e8ab9
Another important point: You are getting a 403 Forbidden error on the URL you provided. So typically, you will not obtain the JSON you wanted.
As I explained in the comment below, I think that you will not be able to access this page without having a fresh URL with valid query parameters and/or cookies. I imagine you obtained this URL from somewhere and it is no longer valid. This is why you'll probably need to use cURL to visit the website with a session to obtain the fresh URL to the JSON API. Use Google to find some examples of PHP scraping/crawling with session handling. You'll see that depending on the website it can get rather tricky, especially if some JavaScript comes into the game.
I use php pretty badly. I'm not a programmer just doing something for my private things.
I have such a problem I would like to download data from my PV production. The data is in the form as below. How to download data from a url in php, respectively, to group them and send them to the array ??
Thank you in advance for all your help.
{"sid":62923,"dataunit":"kWh","data":[{"time":"2019-08-01","no":"1","value":"27.7"},{"time":"2019-08-02","no":"2","value":"24.0"},{"time":"2019-08-03","no":"3","value":"19.9"},{"time":"2019-08-04","no":"4","value":"25.3"},{"time":"2019-08-05","no":"5","value":"0.0"},{"time":"2019-08-06","no":"6","value":"0.0"},{"time":"2019-08-07","no":"7","value":"0.0"},{"time":"2019-08-08","no":"8","value":"0.0"},{"time":"2019-08-09","no":"9","value":"0.0"},{"time":"2019-08-10","no":"10","value":"0.0"},{"time":"2019-08-11","no":"11","value":"0.0"},{"time":"2019-08-12","no":"12","value":"0.0"},{"time":"2019-08-13","no":"13","value":"0.0"},{"time":"2019-08-14","no":"14","value":"0.0"},{"time":"2019-08-15","no":"15","value":"0.0"},{"time":"2019-08-16","no":"16","value":"0.0"},{"time":"2019-08-17","no":"17","value":"0.0"},{"time":"2019-08-18","no":"18","value":"0.0"},{"time":"2019-08-19","no":"19","value":"0.0"},{"time":"2019-08-20","no":"20","value":"0.0"},{"time":"2019-08-21","no":"21","value":"0.0"},{"time":"2019-08-22","no":"22","value":"0.0"},{"time":"2019-08-23","no":"23","value":"0.0"},{"time":"2019-08-24","no":"24","value":"0.0"},{"time":"2019-08-25","no":"25","value":"0.0"},{"time":"2019-08-26","no":"26","value":"0.0"},{"time":"2019-08-27","no":"27","value":"0.0"},{"time":"2019-08-28","no":"28","value":"0.0"},{"time":"2019-08-29","no":"29","value":"0.0"},{"time":"2019-08-30","no":"30","value":"0.0"},{"time":"2019-08-31","no":"31","value":"0.0"}]}
Try this
<?php
$json_url = file_get_contents('Your url goes here');
$data = json_decode($json_url,true);
var_dump($data);
// You can print all of your data here to see if it works
foreach ($data as $items) {
// You can loop trough the data and get it like $items->sid etc
var_dump($items);
}
?>
You need to grab the URL with 'file_get_contents' and than you need to decode it with json_decode
and then you print it out with the for each function
I am making a price crawler for a project but am running into a bit of an issue. I am using the below code to extract values from an html page:
$content = file_get_contents($_POST['url']);
$resultsArray = array();
$sqlresult = array();
$priceElement = explode( '<div>value I want to extract</div>' , $content );
Now when I use this to get certain elements I only get back
Finance: {{value * value2}}
I want to get the actual value that would be displayed on the screen e.g
Finance: 7.96
The other php methods I have tried are:
curl
file_get_html(using simple_html_dom library)
None of these work either :( Any ideas what I can do?
You just set the <div>value I want to extract</div> as a delimiter, which means PHP looks for it to separate your string to array whenever this occurs.
In the following code we use , character as a delimiter:
<?php
$string = "apple,banana,lemon";
$array = explode(',', $string);
echo $array[1];
?>
The output should be this:
banana
In your example you set the value you want to extract as a delimiter. That's why this happens to you. You'll need to set a delimiter between your string you want to obtain and other string you won't need at the moment.
For example:
<?php
$string = "iDontNeedThis-dontExtractNow-value I want to extract-dontNeedEither";
$priceElement = explode('-', $string);
echo "<div>".$priceElement[2]."</div>";
?>
The code should output this to your HTML page:
<div>value I want to extract</div>
And it will appear on your page like this:
value I want to extract
If you don't need to save the whole array in a variable, you can save the one index of it to variable instead:
$priceElement = explode('-', $string)[2];
echo $priceElement;
This will save only value I want to extract so you won't have to deal with arrays later on.
I get a lot of DMCA removal emails for my website, and I'm trying to automate the process of removing those tracks from my website.
The emails all come looking similar to this.
http://example.com/title-to-post.html title - to post
http://example.com/title-of-post.html title - of post
http://example.com/some-song-artist-some-song-name.html some song artist - some song name
But there's a lot of them, I only wanna return the URL portion of every part of this, example being below.
http://example.com/title-to-post.html
http://example.com/title-of-post.html
http://example.com/some-song-artist-some-song-name.html
EDIT: I am storing these files into a txt file, then calling them using standard code.
$urls = file_get_contents( "oururls.txt" );
$data = explode(",", $urls);
foreach ($data as $item) {
echo "$item";
echo '<br>';
}
Nothing really fancy going on, how ever it's returning the title also and I want just the urls.
If there's always a space after the URL you can explode the text by " " and get the first portion. Example:
$example = explode(" ", $url);
echo $example[0];
I need to retrieve data from the following URL:
http://lt.ff.ryanair-bilietai.lt.eturas.lt/flights/webservices/lowest/?limit=50&print=0&way_type=one_way
I need to show all departures cities, name with price, from "departureCity":"Kaunas" and format that data as a list,
The list would look something like this:
<ul>
<li>City Price</li>
<li>Vilnius 59 Lt</li>
</ul>
What is needed to actually pull the JSON data from the external URL?
Once pulled how do I manipulate that data so a list can be generated?
Here is an example of how you can get the data from the json source:
$jsonData = json_decode(file_get_contents("http://lt.ff.ryanair-bilietai.lt.eturas.lt/flights/webservices/lowest/?limit=50&print=0&way_type=one_way"));
echo "<ul>";
foreach($jsonData as $key=>$value){
if($value->departureCity == "Kaunas"){
echo "<li>" . $value->priceAdult . "</li>";
}
}
echo "</ul>";
This will print all the city prices originating from Kaunas. For more info, use this after line 1:
print_r($jsonData);
try
<?php
$data = file_get_contents("http://lt.ff.ryanair-bilietai.lt.eturas.lt/flights/webservices/lowest/?limit=50&print=0&way_type=one_way");
$jsondata = json_decode($data);
// this will print array you can play with it now
print_r($jsondata);
?>
You can get the data from url like this:
$limit = $_GET["limit"];
$print = $_GET["print"];
$way_type = $_GET["way_type"];
These are the values from the url string, you can use them to search from the database or any data source you want.