Ltes say i want to copy a text from site ( http:// www.example.com/ex )
when i open http:/ /www.example.com/ex it shows this data click here
i want to copy every text after MrsId which is in between the double quotes(means i want to copy MN4D / CN4D / MK4D / MO4D all othese four codes from example.com/ex and these codes changes daily )
and sotre in a varible eg ( $a= 'MN4D', $b='CN4D' ,$c='MK4D' ,$d='MO4D' )
then want to use it as
$first = 'http:// www.example.com/?code=';
$url = "{$first}{$a}";
$urll = "{$firsts}{$b}";
$urlll = "{$firsts}{$c}";
$urlllll = "{$firsts}{$d}";
$result = file_get_contents($url);
$results = file_get_contents($urll);
$resultss = file_get_contents($urlll);
$resultsss = file_get_contents($urllll);
echo $result;
echo $results;
echo $resultss;
echo $resultsss;
I am serching for this code from 1 month but did't get success yet.
Change the code for your needs:
<?php
$json = '{"Amrlist":[{"Amcd":"mr-pub-4925511/1986","MrsId":"MN4D","BiclMine":"90","ImagePath":"http://my.example.com/myex/myex.jpg"},{"Amcd":"mr-pub-4925511/1986","MrsId":"CN4D","BiclMine":"90","ImagePath":"http://my.example.com/myex/myex.jpg"},{"Amcd":"mr-pub-4925511/1986","MrsId":"MK4D","BiclMine":"90","ImagePath":"http://my.example.com/myex/myex.jpg"},{"Amcd":"mr-pub-4925511/1986","MrsId":"MO4D","BiclMine":"90","ImagePath":"http://my.example.com/myex/myex.jpg"}]}';
$decoded = json_decode($json, true);
//var_dump($decoded);
$url = 'http://www.example.com/?code=';
foreach($decoded{'Amrlist'} as $Amrlist) {
//print_r($Amrlist);
print $url.$Amrlist['MrsId']."\n";
//print file_get_contents($url.$Amrlist['MrsId']);
}
/**/
?>
As I can see - data has json format. So you can use json_decode to access needed fields
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 got Source Code From Remote Url Like This
$f = file_get_contents("http://www.example.com/abc/");
$str=htmlspecialchars( $f );
echo $str;
in that code i want to replace/extract any url which is like
href="/m/offers/"
i want to replace that code/link as
href="www.example.com/m/offers/"
for that i used
$newstr=str_replace('href="/m/offers/"','href="www/exmple.com/m/offers/',$str);
echo $newstr;
but this is not replacing anything now i want to know 1st ) can i replace by str_replace ,in the code which is fetched from remote url and if 'yes' how ...? if 'no' any other solution ?
There will not be any " in your $str because htmlspecialchars() would have converted them all to be " before it got to your str_replace.
I start assuming all href attributes belong to tags.
Since we know if all tags are written in the same way. instead of opting for regular expressions, I will use an interpreter to facilitate the extraction process
<?php
use Symfony\Component\DomCrawler\Crawler;
$base = "http://www.example.com"
$url = $base . "/abc/";
$html = file_get_contents($url);
$crawler = new Crawler($html);
$links = array();
$raw_links = array();
$offers = array();
foreach($crawler->filter('a') as $atag) {
$raw_links[] = $raw_link = $atag->attr('href');
$links[] = $link = str_replce($base, '', $raw_link);
if (strpos($link, 'm/offers') !== false) {
$offers[] = $link;
}
}
now you have all the raw links, relative links and offerslinks
I use the DomCrawler component
I am building a site-dictionary where you can search for more than one word at a time. I have a button to add inputs, one for each term. Now, i use those inputs and through a dictionary site (legally) i obtain their definitions and apply my own css style to them. So, when you type your word in input 1 (let's call it the like that) you get its definition in a div next to the input. So i have one variable to request the word, four others for the fetching and styling, an one last "echo" for the output. Here is the code:
enter code <?php
$data = preg_replace('/(search?[\d\w]+)/','http://lema.rae.es/drae/srv/\1', $data);
$word = $_REQUEST['word'];
$word2 = $_REQUEST['word2'];
$url = "http://lema.rae.es/drae/srv/search?val={$word}";
$url2 = "http://lema.rae.es/drae/srv/search?val={$word2}";
$css = <<<EOT
<style type="text/css">
</style>
EOT;
$data = file_get_contents($url);
$data2 = file_get_contents($url2);
$data = str_replace('<head>', $css.'</head>', $data);
$data2 = str_replace('<head>', $css.'</head>', $data2);
$data = str_replace('<span class="f"><b>.</b></span>', '', $data);
$data2 = str_replace('<span class="f"><b>.</b></span>', '', $data2);
echo '<div id="result1"
style="">
'.$data.'
</div>';
echo '<div id="result1"
style="">
'.$data2.'
</div>';
?>
Issue: how can i automatically generate this varibles (actually, the process itself) for every new input added?
Arrays are what you're looking for. Instead of creating a new variable $data{INDEX} you can create one variable that can hold a range of variables.
For example, if you want to 'push' an array with data you can do this.
$myData = array();
// appends the contents to the array
$myData[] = file_get_contents($url);
$myData[] = file_get_contents($url2);
Arrays allow for more versatility and efficiency.
You can find the documentation here.
A full implementation would look something like this.
// create an array of requests that we want
// to load in the url.
$words = array('word', 'word2');
// we'll use this later on for loading the files.
$baseUrl = 'http://lema.rae.es/drae/srv/search?val=';
// string to replace in the head.
$cssReplace = '<style type="text/css"></style></head>';
// string to remove in the document.
$spanRemove = '<span class="f"><b>.</b></span>';
// use for printing out the result ID.
$resultIndex = 0;
// loop through the words we defined above
// load the respective file, and print it out.
foreach($words as $word) {
// check if the request with
// the given word exists. If not,
// continue to the next word
if(!isset($_REQUEST[$word]))
continue;
// load the contents of the base url and requested word.
$contents = file_get_contents($baseUrl . $_REQUEST[$word]);
// replace the data defined above.
$contents = str_replace('</head>', $cssReplace, $contents);
$contents = str_replace($spanRemove, '', $contents);
// print out the result with the result index.
// ++$resultIndex simply returns the value of
// $resultIndex after adding one to it.
echo '<div id="result', (++$resultIndex) ,'">', $contents ,'</div>';
}