How can I get contents from 2 urls by file_get_contents(); at the same time?
$url1 ="https://site1.com";
$url2 ="https://site2.com";
$urls = file_get_contents($url1 + $url2);
echo $urls;
You can't, but you can get the first and then the second and append the contents to the first:
$urls = file_get_contents($url1) . file_get_contents($url2);
Or:
$urls = file_get_contents($url1);
$urls .= file_get_contents($url2);
If you have many URLs then create an array and loop them:
$urls = ["https://site1.com", "https://site2.com"];
$result = '';
foreach($urls as $url) {
$result .= file_get_contents($url);
}
Related
I want to turn the variable into an array so I can store more than one feed?
<?php
error_reporting(0);
$feed_lifehacker_full = simplexml_load_file('http://feeds.gawker.com/lifehacker/full');
$xml = $feed_lifehacker_full;
//print_r($xml);
foreach ($xml->channel->item as $node){
$title = $node->title;
$link = $node->link;
$link = explode('/', $link);
$link = $link[8];
$url = $node->url;
$description = $node->description;
$pubDate = $node->pubDate;
preg_match_all('#(http://img[^\s]+(?=\.(jpe?g|png|gif)))#i', $description[0], $images);
$images = $images[0][1] . '.jpg';
if($images == '.jpg'){
//uncomment to show youtube articles
//$images = "http://placehold.it/640x360";
//echo "<a href='page2.php?a=$link' title='$title'><img src='$images' /></a><br>";
} else {
//article image
$images . '<br>';
echo "<a href='page2.php?a=$link' title='$title'><img src='$images' /></a><br>";
}
}
How can I change this to load to arrays,
$feed_lifehacker_full = simplexml_load_file('http://feeds.gawker.com/lifehacker/full');
$xml = $feed_lifehacker_full;
The script is just gathering the image of an rss feed and linking to a page, if you see how it can be done more efficiently feel free to say
it is possible to encode the result given as json and by decoding it it will return you an array
$xml = simplexml_load_string($xmlstring);
$json = json_encode($xml);
$array = json_decode($json, TRUE);
I have this url /index.php?color=blue&size=xl
to get rid of the get parameter, I use this code:
$done = preg_replace('/(.*)(\?|&)color=[^&]*(?(1)&|)?/i', "$1", $url);
echo $done;
"output: index.phpsize=xl"
Now I need to clean the "size" part too. Have tried with two lines of preg_replace, but it doesn´t work.
$done = preg_replace('/(.*)(\?|&)color=[^&]*(?(1)&|)?/i', "$1", $url);
echo $done;
$done2 = preg_replace('/(.*)(\?|&)size=[^&]*(?(1)&|)?/i', "$1", $done);
Edit: I really need a solution where I can clean the exact parameter "color" or "size".
Sometimes I will only delete one of them.
Edit2:
Have this solution:
// Url is: index.php?color=black&size=xl&price=20
function removeqsvar($url, $varname) {
return preg_replace('/([?&])'.$varname.'=[^&]+(&|$)/','$1',$url);
}
$url = removeqsvar($url, color);
echo removeqsvar($url, price);
// will output: index.php?size=xl
Thank you all.
This will allow you to exactly specify which parameters to remove using the $remove array. It works by parsing the URL with parse_url(), then grabbing the query string and parsing it with parse_str().
From there, it's straightforward - Iterate over the parameters in the URL, if one of them is in the $remove array, then delete it from the $params array. By the end, if we have parameters to add to the URL, we add them back with http_build_query().
$url = '/index.php?color=blue&size=xl'; // Your input URL
$remove = array( 'color', 'size'); // Change this to remove what you want
$parts = parse_url( $url);
parse_str( $parts['query'], $params);
foreach( $params as $k => $v) {
if( in_array( $k, $remove)) {
unset( $params[$k]);
}
}
$url = $parts['path'] . ((count( $params) > 0) ? '?' . http_build_query( $params) : '');
echo $url;
list($done) = explode("?", $url);
This snytax also works in PHP 5.3 and lower
try this:
$result = explode('?', $url)[0];
for a php version lower than php 5.4:
$tmp = explode('?', $url);
$result = $tmp[0];
I am trying with the code below to get a bing rss news feed, grab all the titles from this data to an array and then implode them all together so I have a variable with all the words together in a string so I can then create a word cloud out of this with another peice of code. So far it grabs the rss feed and print_r($doc); if you uncomment it displays the simple xml. Howver my foreach looping to grab the titles in the array doesn't seem to be working and I can't see where the error is? Thanks in advance.
$ch = curl_init("http://api.bing.com/rss.aspx?Source=News&Market=en-GB&Version=2.0&Query=web+design+uk");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$doc = new SimpleXMLElement($data);
//print_r($doc);
$vals = array();
foreach ($doc->entry as $entry) {
$vals[] = (string) $entry->title;
}
//join content nodes together for the word cloud
$vals = implode(' ', $vals);
echo($vals);
The titles are at rss/channel/item/title and not where your code looks for them, at rss/entry/title. Other than that, your way of getting the values is fine.
$rss = simplexml_load_file('http://api.bing.com/rss.aspx?Source=News&Market=en-GB&Version=2.0&Query=web+design+uk');
$titles = array();
foreach ($rss->channel->item as $item) {
$titles[] = (string) $item->title;
}
//join content nodes together for the word cloud
$words = implode(' ', $titles);
echo $words;
A quicky alternative, using XPath to get the titles, is:
$rss = simplexml_load_file('http://api.bing.com/rss.aspx?Source=News&Market=en-GB&Version=2.0&Query=web+design+uk');
$words = implode(' ', $rss->xpath('channel/item/title'));
echo $words;
I have tried googling for the past one hour straight now and tried many ways to search for an array, in an array.
My objective is, to find a keyword in the URL, and the keywords are in a txt file.
This is what i have so far - but doesn't work.
$file = "keywords.txt";
$open = fopen($file,'r');
$data = fread($open,filesize($file));
$data = explode(" ",$data);
$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$url = parse_url($url); //parse the URL into an array
foreach($data as $d)
{
if(strstr($d,$url))
{
echo "yes";
}
}
This works WITHOUT the text file, or array - but that's not what i want.
I'd appreciate it if anyone can assist me.
This is the way I'd do it:
$file = "keywords.txt";
$open = fopen($file,'r');
$data = fread($open,filesize($file));
$data = explode(" ",$data);
$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$url = parse_url($url); //parse the URL into an array
foreach($data as $d){
if(in_array($d,$url)){
echo "yes";
}
}
The xml is like this: (wordpress url's) I want to strip them and get only the posts words.
http://www.site1.com/dir/this-is-page/
http://www.site2.com/this-is-page
How do i strip the url's and get only "this is page" (without the rest of the urls, and the "-") if i have two diffrent types of urls; one with dir and one without dir? Sample code bellow:
$feeds = array('http://www.site1.com/dir/feed.xml', 'http://www.site2.com/feed.xml');
foreach($feeds as $feed)
{
$xml = simplexml_load_file($feed);
foreach( $xml->url as $url )
{
$loc = $url->loc;
echo $loc;
$locstrip = explode("/",$loc);
$locstripped = $locstrip[4];
echo '<br />';
echo $locstripped;
echo '<br />';
mysql_query("TRUNCATE TABLE interlinks");
mysql_query("INSERT INTO interlinks (title, url) VALUES ('$locstripped', '$loc')");
}
}
?>
TY
Ty guys, did it like this:
$urlstrip = basename($loc);
$linestrip = str_replace(array('-','_'), ' ', $urlstrip);
You want only the last segment of the URL?
Try something like this.
$url = trim('http://www.site1.com/dir/this-is-page/', '/');
$url = explode('/', $url);
$url = array_pop($url);
$url = str_replace(array('-','_'), ' ', $url);
It's not very elegant... but it works.
replace
$locstripped = $locstrip[4];
with
$locstripped = $locstrip[count($loc) - 1];
if(!$locstripped)
$locstripped = $locstrip[count($loc) - 2];
$locstripped = str_replace('-', ' ', $locstripped);