PHP foreach null or else? - php

I'm using a basic LAMP server for my website and looking to rewrite a script I have that's linked with Twitch's API. The issue that I'm having is trying to find, for lack of a better word, the opposite of the foreach.
For example I have an array of names that are sent with a URL to the Twitch servers, and if one of the names are currently streaming "streams" will have lots of values, and if they aren't currently streaming "streams" will return null.
This works wonderful for checking people and showing they are online, but I also need it to display the ones who are offline and I can't figure out how to do that. Something like a foreachelse or something like that. This is the code I have below. Thanks.
<?php
$username = array("zeromi", "aerosgw", "krylen","tshirt_aion","snooky_aion","vashiro","papa456","vinley_aion","hanfkrokette","wtfast_siel","neckofthewood","paraproc","aionnae","uhiwi","mufflermankr","valorium","knighterrantry","soulune","relizex3","vinlockz","trevyn201","tiger529","xkegi","logsnsticks","meowform","uzuk3","kalzard01","squall_m","suyji","headpcgamer","sariett_siel");
$callAPI = implode(",",$username);
$data = json_decode(#file_get_contents('https://api.twitch.tv/kraken/streams?channel=' . $callAPI), true);
foreach ($data['streams'] as $streams){
$name = $streams['channel']['name'];
echo $name.'<br>';
}
?>

I don't see the problem, if you just use array_diff, you can filter out the usernames.
$online = array();
foreach ($data['streams'] as $streams)
$online[] = $streams['channel']['name'];
$offline = array_diff($username, $online);
echo 'Online users: ' . implode(', ', $online) . "\n<br>";
echo 'Offline users: '. implode(', ', $offline);
Output (at time of writing):
Online users: sariett_siel, mufflermankr
Offline users: zeromi, aerosgw, krylen, tshirt_aion, snooky_aion, vashiro, papa456, vinley_aion, hanfkrokette, wtfast_siel, neckofthewood, paraproc, aionnae, uhiwi, valorium, knighterrantry, soulune, relizex3, vinlockz, trevyn201, tiger529, xkegi, logsnsticks, meowform, uzuk3, kalzard01, squall_m, suyji, headpcgamer

Related

Get info from API/URL

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.

php goutte web scraper gives different results from time to time

I'm trying to build a web scraper for amazon's product page.
I decided to go with Goutte library (based on this freeCodeCamp tutorial).
Here's what I've coded so far:
<?php
require 'vendor/autoload.php';
$link = readline('Enter the product you want to scrape: ');
$httpClient = new \Goutte\Client();
$response = $httpClient->request('GET', $link);
$title_ = $response->evaluate('//span[#id="productTitle"]');
$price_ = $response->evaluate('//span[#class="priceBlockStrikePriceString a-text-strike"]');
$offer_ = $response->evaluate('//span[#id="priceblock_ourprice"]');
foreach ($title_ as $key => $title) {
$str = $title->textContent . PHP_EOL;
$str = str_replace("\n","",$str);
echo $str , "\n";
}
foreach ($price_ as $key => $price) {
$str = $price->textContent . PHP_EOL;
$str = str_replace(array("\n", " "),"",$str);
echo $str , "\n";
}
foreach ($offer_ as $key => $offer) {
$str = $offer->textContent . PHP_EOL;
$str = str_replace(array("\n", " "),"",$str);
echo $str , "\n";
}
?>
As you can see I'm trying to extract the product's title, listed price and Amazon's offer.
What really frustrates me, is that the above code sometimes works and sometimes it doesn't.
For example, I'm testing it by giving it this link.
Sometimes I get the desired result:
Amazon Basics Liquid Crystal Clear Soft TPU Smartphone Cover for iPhone 13 Pro Max
$9.99
$6.69
But then I'm trying again, without changing anything, and I only get the product's title:
Amazon Basics Liquid Crystal Clear Soft TPU Smartphone Cover for iPhone 13 Pro Max
What's going on? I'm guessing issues with my code (I'm new to web scraping), can anyone help? Thanks in advance.

How can I get just the URL from this?

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];

PHP JSON Parsing Issue

I have this JSON output from a Government API, I need to display it using PHP. The problem is I can't use foreach more then once in a row or it doesn't work. I can't load all the criteria into the first foreach because say the first piece of data ACASS returns 3 results, all the fields after it will be displayed 3 times. Each field could return 1-10 results so there needs to be a system that accounts for variables.
I'm thinking the solution is to put all of the JSON items I need displayed into the first foreach but set them to only display if they're populated. That or use the current coding system I have but account for variable numbers somehow.
Any potential solutions are greatly appreciated.
This is the JSON output... https://api.data.gov/sam/v4/registrations/9606040070000?api_key=WI7nHENlp6QDMnWsb0Nnmzsv1slPDTjNM0XBoKvY
Here's the PHP I'm using...
echo "ACASS ID:".$decoded_results['sam_data']['registration']['qualifications']['acass']['id']."</br>";
foreach($decoded_results['sam_data']['registration']['qualifications']['acass']['answers'] as $acass)
{
echo 'Answer Text:'.$acass['answerText'].'</br>';
echo 'ACASS Section:'.$acass['section'].'</br>';
}
$formerfirm = $decoded_results['sam_data']['registration']['qualifications']['acass']['answers'][2]['FormerFirm'];
echo 'Former Firm ID:'.$formerfirm['id'].'</br>';
echo 'Former Firm Year Established:'.$formerfirm['yearEstablished'].'</br>';
echo 'Former Firm Name:'.$formerfirm['name'].'</br>';
echo 'Former Firm DUNS'.$formerfirm['duns'].'</br>';
I did my best to keep this short and simple question / code wise. In summary the issue is if you look at the JSON the data hierarchy makes a lot of the information display under ACASS/Answers and then the next category. I never know how many responses there will be and I'm not sure how to account for those variables.
I would like to thank everyone on these boards who has guided me as a new member and helped me post cleaner, more concise questions. Also thank you to everyone who has taken their own personal time to help me learn to become a better programmer.
use a tool like http://jsonviewer.stack.hu/ for visualizing your json structure. It helps a lot.
<?php
$url = "https://api.data.gov/sam/v4/registrations/9606040070000?api_key=WI7nHENlp6QDMnWsb0Nnmzsv1slPDTjNM0XBoKvY";
$contents = json_decode(file_get_contents($url));
// echo var_dump($contents);
$sam_data = $contents->sam_data;
// echo var_dump($sam_data);
$registration = $sam_data->registration;
//echo var_dump($registration);
$acass = $contents->sam_data->registration->qualifications->acass;
$id = $acass->id;
echo "id: ". $id . "<br />";
//echo var_dump($acass->answers);
foreach($acass->answers as $answer) {
if(isset($answer->FormerFirm)) {
$formerFirm = $answer->FormerFirm;
echo var_dump($formerFirm);
}
}

Are Facebook Names Publicly Accesible Using Facebook API

Are the names of profiles on Facebook publicly accessible, as if I would NOT need to log into Facebook to access them?
I am intending to store a large amount of names as a small piece of a larger project. I feel as if scraping Facebook for names would be a relatively simple task using the Facebook Graph API, but I am a little confused.
I found another tutorial online at http://jilltxt.net/?p=2810 which described an easy way of finding any Facebook profile picture using one simple line:
https://graph.facebook.com/USER-ID/picture?type=large
This was very helpful because I am able to use a range of ID numbers and a small amount of PHP to gather large amounts of profile pictures as seen on my test page here: http://www.joshiefishbein.com/fi/photobook.php
But what I am unfamiliar with is how I go from collecting pictures to names in this one simple line. Is it possible? Is there another (better) way?
Here's the code I am working with. The range of ID's are just an example.
function gen_pix($min, $max, $quantity) {
$numbers = range($min, $max);
shuffle($numbers);
$x_arr = array_slice($numbers, 0, $quantity);
foreach ($x_arr as $key => $value) {
$username = "https://graph.facebook.com/" . $value . "/";
$json = json_decode(file_get_contents($username), true);
echo $json["name"];
}
}
$x = 337800042;
$y = 337800382;
$z = 1;
gen_pix($x,$y,$z);
I've gotten a little farther with this code, I can echo $username and I get the URL that I am looking for (for example https://graph.facebook.com/337800382/) but I do not get anything after that. json_decode isn't working seemingly.
In the same way you are pulling the profile picture, you can get the basic information of a user with their ID.
This page provides a list of data that is always publicly accessible.
So you need to make a GET request to pull back the JSON, like so...
https://graph.facebook.com/{user-id}/
For example https://graph.facebook.com/586207189/ pulls back my basic information. So your PHP would look like this
$json = json_decode(file_get_contents("https://graph.facebook.com/$user_id/"), true);
echo $json["name"];
PHP fiddle here
Update: Based on the code above, it's worth adding an IF to catch invalid Facebook IDs. Facebook IDs may not be sequential so not every one will return a name or image.
Updated code:
<?php
function gen_pix($min, $max, $quantity) {
$numbers = range($min, $max);
shuffle($numbers);
$x_arr = array_slice($numbers, 0, $quantity);
foreach ($x_arr as $key => $value) {
$username = "https://graph.facebook.com/" . $value . "/";
$json = json_decode(file_get_contents($username), true);
if (!isset($json['name'])) {
echo "Invalid ID<br />";
}
else {
echo $json["name"]. '<br />';
}
}
}
$x = 337800042;
$y = 337800382;
$z = 50;
gen_pix($x,$y,$z);
?>
PHP Fiddle here
It's also worth noting that pulling that much data from the graph is going to take a while. Have a look at doing batch requests to speed things up a bit. More info here

Categories