I have this script to get google indexed pages of a url :
function getGoogleCount($domain) {
$content = file_get_contents('http://ajax.googleapis.com/ajax/services/' .
'search/web?v=1.0&filter=0&q=site:' . urlencode($domain));
$data = json_decode($content);
return intval($data->responseData->cursor->estimatedResultCount);
}
echo getGoogleCount('http://stackoverflow.com/');
But it not let me to get data for more urls. Then i used some online tools and result is different from mine. Is there other way to get these data without restrictions ?
Thanks.
What about calling getGoogleCount in a loop?
$domains = array('domain 1', 'domain 2');
foreach ($domains as $domain) {
echo getGoogleCount($domain);
}
Related
I'm having an issue with php file_get_content(), I have a txt file with links where I created a foreach loop that display multiple links in the same webpage but it's not working, please take a look at the code:
<?php
$urls = file("links.txt");
foreach($urls as $url) {
file_get_contents($url);
echo $url;
}
The content of links.txt is: https://www.google.com
Result: Only a String displaying "https://www.google.com"
Another code that works is :
$url1 = file_get_contents('https://google.com');
echo $url1;
This code returns google's homepage, but I need to use first method with loops to provide multiple links.
Any idea?
Here's one way of combining the things you already had implemented:
$urls = file("links.txt");
foreach($urls as $url) {
$contents = file_get_contents($url);
echo $contents;
}
Both file and file_get_contents are functions that return some value; what you had to do is putting return value of the latter one inside a variable, then outputting that variable with echo.
In fact, you didn't even need to use variable: this...
$urls = file("links.txt");
foreach($urls as $url) {
echo file_get_contents($url);
}
... should have been sufficient too.
If you open the URL, you'll see that it's a very long string of sub objects. I want to extract the values for the 70 position. So far I've been able to extract the first tree without a problem ... But if you go deeper then I don’t get any feedback at all. Please check the code below and tell me, what am I doing wrong?
$url= "https://bwt.cbp.gov/api/waittimes";
$port = file_get_contents($url); // put the contents of the file into a variable
$data = json_decode($port); // decode the JSON feed
echo $data[69]->port_name.'<br>';
echo $data[69]->port_status.'<br>';
echo $data[69]->passenger_vehicle_lanes->maximum_lanes.'<br>';
echo $data[69]->passenger_vehicle_lanes->standard_lanes->lanes_open.'<br>';
The following is working for me:
$url= "https://bwt.cbp.gov/api/waittimes";
$port = file_get_contents($url); // put the contents of the file into a variable
$data = json_decode($port, true); // decode the JSON feed
echo "There are ".count($data)."Ports".PHP_EOL;
$found=false;
foreach ($data as $key => $value) {
//EDIT AFTER COMMENT**
if($value['port_number']==250401){
echo $value['port_name'].' '.$value['crossing_name'].PHP_EOL;
$found=true;
break;
}
}
if(!$found) echo "couldn't find port #";
can you try to change json_decode($port, true); (true will change object to array and it will be better to access it) like this and access it like in array echo $data[69]['passenger_vehicle_lanes']['maximum_lanes'].'<br>';
is there a way to get instagram page the contents of an post using simple php
i did some search and i found this script
$url = 'https://www.instagram.com/pagename/';
$str = file_get_contents($url);
$count = 0;
if(preg_match('#followed_by": {"count": (.*?)}#', $str, $match)) {
$count = $match[1]; // get the count from Regex pattern
}
echo $count;
but it is getting only number of follower is there a way
to get the contents of an Instagram post using same concept ?
Here's a code that works (today). But as #Andy said, it's not reliable and it's dirty af :)
<?php
$source = file_get_contents("https://www.instagram.com/p/POST_ID/");
preg_match('/<script type="text\/javascript">window\._sharedData =([^;]+);<\/script>/', $source, $matches);
if (!isset($matches[1]))
return false;
$r = json_decode($matches[1]);
print_r($r);
// Example to get the likes count
// $r->entry_data->PostPage[0]->graphql->shortcode_media->edge_media_preview_like->count
i took #Andy advice as he it's not reliable and it's dirty af
So this is what i found to go over the html
Instagram change their page markup, your application will break.
is this
$username = 'username';
$instaResult=
file_get_contents('https://www.instagram.com/'.$username.'/media/');
//decode json string into array
$data = json_decode($instaResult);
foreach ($data as $posts) {
foreach($posts as $post){
$postit = (array) json_decode(json_encode($post), True);
/* get post text and image */
echo '<p>' .$postit["caption"]["text"].'</p>';
echo '<img src="'.$postit["images"]["standard_resolution"]["url"].'" />';
echo "</br>-----------</br>";
}
}
I have array, for example:
$links = array(
'http://aaa.com/data.txt',
'http://aaea.com/data.txt',
'http://aada.com/data.txt',
'http://agaa.com/data.txt',
'http://ahaa.com/data.txt',
'http://awha.com/data.txt',
'http://aaeha.com/data.txt',
//etc x100
);
And in PHP I am doing:
foreach ($links as $link) {
$data = file_get_contents($link);
//save data in database
}
It works fine, but terribly slow. How is better way for this with PHP? I would like get data asynchronous.
My other way - jQuery and Ajax queries from PHP script, but maybe exists better way?
I would suggest doing it this way.
<?php
$Links = array(
'http://aaa.com/data.txt',
'http://aaea.com/data.txt',
'http://aada.com/data.txt',
'http://agaa.com/data.txt',
'http://ahaa.com/data.txt',
'http://awha.com/data.txt',
'http://aaeha.com/data.txt'
);
$TempData = '';
foreach ($Links as $Link) {
$TempData .= file_get_contents($Link);
$TempData .= '|';
}
$Data = rtrim($TempData, '|');
// save the $Data string and when you export the
// string from the db use this code to turn it into an array
//
// $Data = explode('|' $ExportedData);
// var_dump($Data);
//
// If you do it this way you will be preforming 1 sql
// statement instead of multiple saving site resources
// and making you code execute faster
?>
If this helped let me know.
I have been researching this topic for a few days now and i'm still non the wiser as on how to do it.
I want to get an RSS feed from forexfactory.com to my website, i want to do some formatting on whats happening and i also want the latest information from them (Although those last two points can wait as long as i have some more or feed running).
Preferably I'd like to develop this from the ground up if anyone knows of a tutorial or something i could use?
If not i will settle for using a third party API or something like that as long as i get to do some of the work.
I'm not sure what it is but there is something about RSS that i'm not getting so if anyone knows of any good, probably basic tutorials that would help me out a lot. It's kind of hard going through page after page of google searches.
Also i'm not to fussed on the language it's outputted in Javascript, PHP or HTML will be great though.
Thanks for the help.
It looks like SimplePie may be what you are looking for. It's a very basic RSS plugin which is quite easy to use and is customisable too. You can download it from the website.
You can use it at it's bare bones or you can delve deeper in to the plugin if you wish. Here's a demo on their website.
index.php
include('rss_class.php');
$feedlist = new rss($feed_url);
echo $feedlist->display(2,"Feed Title");
rss_class.php
<?php
class rss {
var $feed;
function rss($feed){
$this->feed = $feed;
}
function parse(){
$rss = simplexml_load_file($this->feed);
//print_r($rss);die; /// Check here for attributes
$rss_split = array();
foreach ($rss->channel->item as $item) {
$title = (string) $item->title;
$link = (string) $item->link;
$pubDate = (string) $item->pubDate;
$description = (string) $item->description;
$image = $rss->channel->item->enclosure->attributes();
$image_url = $image['url'];
$rss_split[] = '
<li>
<h5>'.$title.'</h5>
<span class="dateWrap">'.$pubDate.'</span>
<p>'.$description.'</p>
Read Full Story
</li>
';
}
return $rss_split;
}
function display($numrows,$head){
$rss_split = $this->parse();
$i = 0;
$rss_data = '<h2>'.$head.'</h2><ul class="newsBlock">';
while($i<$numrows){
$rss_data .= $rss_split[$i];
$i++;
}
$trim = str_replace('', '',$this->feed);
$user = str_replace('&lang=en-us&format=rss_200','',$trim);
$rss_data.='</ul>';
return $rss_data;
}
}
?>
I didn't incorporate the < TABLE > tags as there might be more than one article that you would like to display.
class RssFeed
{
public $rss = "";
public function __construct($article)
{
$this->rss = simplexml_load_file($article, 'SimpleXMLElement', LIBXML_NOERROR | LIBXML_NOWARNING);
if($this->rss != false)
{
printf("<TR>\r\n");
printf("<TD>\r\n");
printf("<h3>%s</h3>\r\n", $this->rss->channel->title);
printf("</TD></TR>\r\n");
foreach($this->rss->channel->item as $value)
{
printf("<TR>\r\n");
printf("<TD id=\"feedmiddletd\">\r\n");
printf("<A target=\"_blank\" HREF=\"%s\">%s</A><BR/>\r\n", $value->link, $value->title);
printf($value->description);
printf("</TD></TR>\r\n");
}
}
}
}