foreach arrays? - php

Script:
include_once 'simple_html_dom.php';
$ckfile = 'cookie.txt';
foreach (range('a', 'z') as $letters) {
echo $letters;
}
foreach (range('1', '100') as $numbers) {
echo $numbers;
}
$ch = curl_init ("http://site/test.php?letter=".$letters."&page=".$numbers."");
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$output = curl_exec ($ch);
$html = str_get_html(''.$output.'');
foreach($html->find('a') as $element) {
echo $element->href . '<br>';
}
As you can see there is three arrays seperated, one by one works, but how them 'put together' in one foreach and do 'job' ?
Etc. I'm not familiar with foreach after all, and if i remember correctly, it's my second time with 'them'...

foreach (array_merge($arr1, $arr2, $arr3) as element) { /* do something */ }
array_merge()

include_once 'simple_html_dom.php';
$ckfile = 'cookie.txt';
foreach (range('a', 'z') as $letters) {
echo $letters;
foreach (range('1', '100') as $numbers) {
echo $numbers;
$ch = curl_init ("http://site/test.php?letter=".$letters."&page=".$numbers."");
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$output = curl_exec ($ch);
$html = str_get_html(''.$output.'');
foreach($html->find('a') as $element) {
echo $element->href . '<br>';
}
}
echo $numbers;
}
But this script will execute for a few minutes... What do you need it for?

Related

Instagram Story Scraper

i built a Instagram Story Scraper, but its not working...
I've tried everything, but there's something wrong with my code
I hope you can help me
this is my index.php
<?php
require_once('instagramStory.php');
$story = new instagram_story();
echo $story->getStory("garyvee");
?>
and this is my instagramStory.php
<?php
class instagram_story{
protected function file_get_contents_curl($url){
$cookies = dirname(__FILE__)."/cookie.txt" ;
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($curl, CURLOPT_COOKIEFILE, $cookies);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, true);
$answer = curl_exec($curl);
curl_close($curl);
return $answer;
}
public function getStory($username){
$url = file_get_contents("https://www.instagram.com/$username/");
$json = '/sharedData\s=\s(.*[^\"]);<.script>/ixU';
preg_match_all($json, $url, $jsondata, PREG_SET_ORDER, 0);
$array = json_decode($jsondata[0][1], true);
$user_id = $array['entry_data']['ProfilePage']['0']['graphql']['user']['id'];
$stories = $this->file_get_contents_curl("https://www.instagram.com/graphql/query/?query_hash=de8017ee0a7c9c45ec4260733d81ea31&variables=%7B%22reel_ids%22%3A%5B%22$user_id%22%5D%2C%22tag_names%22%3A%5B%5D%2C%22location_ids%22%3A%5B%5D%2C%22highlight_reel_ids%22%3A%5B%5D%2C%22precomposed_overlay%22%3Afalse%2C%22show_story_viewer_list%22%3Atrue%2C%22story_viewer_fetch_count%22%3A50%2C%22story_viewer_cursor%22%3A%22%22%7D");
$data = json_decode($stories, true);
var_dump($data); die();
$_story = [];
foreach ($stories as $story) {
$vid = 'video_resources';
if (!array_key_exists($vid, $story)) {
$_story [] = $story['display_url'];
} else {
$_story [] = $story['video_resources'][0]['src'];
}
}
foreach ($_story as $story) {
$check = '/mp4/m';
preg_match_all($check, $story, $matches, PREG_SET_ORDER, 0);
if (empty($matches)) {
echo "<a href=$story&dl=1><img src=$story></a>";
} else {
echo '<video width="320" height="240" controls>';
echo '<source src="' . $story . '" type="video/mp4">';
echo '</video>';
echo "<a href=$story&dl=1>Download</a>";
}
}
}
}
?>
this is the error code I get

"Invalid argument supplied for foreach()" reading XML?

IM gettin: "Invalid argument supplied for foreach()"
in the for each line when trying to read an specific item of the xml
PHP:
$urls="http://www.floatrates.com/daily/usd.xml";
$url= $urls;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$feed = new SimpleXMLElement($data,LIBXML_NOCDATA);
foreach ($feed->channel->item as $item) {
$title = $item->title;
if (preg_match('/ARS/',$title)) {
echo $title;
}
}
$urls = "http://www.floatrates.com/daily/usd.xml";
$url = $urls;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$feed = new SimpleXMLElement($data, LIBXML_NOCDATA);
foreach ($feed->children() as $item) {
if ("item" == $item->getName()) {
$title = $item->title;
if (preg_match('/ARS/', $title)) {
echo $title;
}
}
}
It probably means that $feed does not contain what you expect it to contain, and you are not passing array to foreach Use print_r($feed); to see that it actually contains, and go from there.

removing a "," from the last element in a foreach loop?

i am trying (like hell..) to remove ',' from the last element in a foreach loop.
tried several counter methods, but not really successfully.
[<?php
function fetchData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$result = fetchData("https://api.instagram.com/v1/users/XXXXXXX/media/recent/?access_token=XXXXXXX&count=10");
$result = json_decode($result);
foreach ($result->data as $post) {
echo '{"name":"Hello","imgpath":" ';echo $post->images->low_resolution->url;echo '"},';
}
?>]
Use json_encode()
<?php
...
$json = array();
foreach ($result->data as $post){
$json[]=array(
'name'=>'Hello',
'imgpath'=>$post->images->low_resolution->url,
);
}
header('Content-Type: application/json');
exit(json_encode($json));
?>
Store the whole thing in a string instead of echoing it, then you can cut the last character using $string=substr($string,0,-1);
[<?php
function fetchData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$result = fetchData("https://api.instagram.com/v1/users/XXXXXXX/media/recent/?access_token=XXXXXXX&count=10");
$string="";
$result = json_decode($result);
foreach ($result->data as $post) {
$string.='{"name":"Hello","imgpath":"' . $post->images->low_resolution->url . '"},';
}
echo substr($string,0,-1);
?>]
Maybe something like:
// quick and dirty solution
foreach ($result->data as $index => $post) {
$comma = ',';
if ((int) $index === count($result->data) -1) {
$comma = '';
}
$string.='{"name":"Hello","imgpath":" ';echo $post->images->low_resolution->url;echo '"}'.$comma;
}
I would use implode, quite cleaner:
$result = json_decode($result);
$string = '{"name":"Hello","imgpath":"';
$string .= implode( '}, {"name":"Hello","imgpath":"', $result->data );
$string .= '}';
echo $string;

json decode into array

I'm trying to store the titles in an array and extract the long cmcontinue string from the below url.
http://en.wikipedia.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:1980_births&format=json
my current code:
$url = 'http://en.wikipedia.org/w/api.php?
action=query&list=categorymembers&cmtitle=Category:'.$cat.'&format=json';
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, "asdf");
$c = curl_exec($ch);
$json = json_decode($c);
$array = $json->{'query'}->{'categorymembers'}->{'title'};
try adding second paremeter of json_decode, like:
$json = json_decode($c, true);
And get cmcontinue value as:
echo $json["query-continue"]["categorymembers"]["cmcontinue"];
For titles:
$titles = array();
foreach($json["query"]["categorymembers"] as $vals) {
array_push($titles, $vals["title"]);
}
echo "<pre>"; print_r($titles);

How to extract and assign data from cURL output?

The code:
function GetCurlPage ($pageSpec)
{
$ch = curl_init($pageSpec);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$tmp = curl_exec ($ch);
curl_close ($ch);
$tmp = preg_replace('/(?s)<meta http-equiv="Expires"[^>]*>/i', '', $tmp);
return $tmp;
}
$url = "https://www.domain.com/check.php?domain=" .
$domain . "&suffixes=" . $suffixes . "&fuzzysearch=" . $fuzzysearch;
$output = GetCurlPage("$url");
print $output;
It outputs 3 lines of html, however I wanted to assign a variable to each line to then do something with it...
Ie:
if ($line_1_of_output=="Hi")
{
"do something"
}
if ($line_2_of_output=="Hi")
{
"do something else"
}
How can I output each line in it's own variable in XML if this is the best way to go about it?
Thanks!
Unless I am misunderstanding you, you can just explode cURL's output and then loop through the array.
<?php
// cURL code...
$tmp = explode("<br>", $tmp);
foreach ($tmp AS $lnum => $line) {
var_dump($line);
echo '<br>';
}
Try this:
function GetCurlPage ($pageSpec)
{
$ch = curl_init($pageSpec);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$tmp = curl_exec ($ch);
curl_close ($ch);
$tmp = preg_replace('/(?s)<meta http-equiv="Expires"[^>]*>/i', '', $tmp);
$tmp = explode('<br>', $tmp);
// Test the array.
foreach ($tmp AS $line) {
echo '<pre>';
print_r($line);
echo '</pre>';
}
// Do something with each line.
$tmp[0] = str_replace('foo', 'bar', $tmp[0]);
$tmp[1] = str_replace('animal', 'monkey', $tmp[1]);
$tmp[2] = str_replace('cat', 'dog', $tmp[2]);
return $tmp;
}

Categories