问题
Is there a way to pass a variable in a URL with file_get_contents() and have file_get_contents() retrieve dynamic content that is based on the value of the variable passed?
For example, let's say I have the following code on a page on Website A:
$contents=file_get_contents('http://example.com/get.php?a='.$number);
echo $contents;
where $number
is generated on Website A (values can be 1, 2, 3, etc.)
Then on example.com, get.php
is hosted. Is it possible to retrieve different content from get.php
based on the value of the variable passed? For example, if a=1
then a certain part of get.php
would be fetched, but if a=2
and different part of get.php
would be fetched, etc. Is this possible?
回答1:
Certainly.
It is possible if you figure out the pattern for the variables.
If you notice that the pattern is a number you might try something like this:
You don't want to show anything that's not going to be pertinent in the code or output and you don't want to get into filtering on the fly because it will only add delay. In get.php
on the target machine:
if(!empty($_GET['a']) and is_numeric($_GET['a'])){
$id = $_GET['a'];
$sql = "select contents from database where id='$id'";
$results = mysqli_query($sql);
$row_cnt = mysqli_num_rows($result);
if ($row_cnt == 1){
while ($row = mysqli_fetch_array($results)){
echo $row['contents'];
}
} elseif (empty($row_cnt) {
echo "No results";
} else {
echo "Too many records.";
}
} else {
die;
}
In the machine doing the mining (machine 2):
$contents = '';
for($x=0;$x<150;$x++){
$contents.=file_get_contents('http://example.com/get.php?a='.$number);
}
echo $contents;
Remember when opening a URL over the web that you need to:
- grant allow_url_fopen access if necessary
- add a delay so you're not overwhelming the target server's resources
- verify that you're not in violation of someone's robots.txt file
- pass proper headers including user_agent so your server is not banned
- increase the run time limits if you're running the script as a webpage under Apache.
cURL is much more appropriate for this and contains many advanced features.
Update - but it's probably not a good idea
The above example would be for just one or two pages, not for viewing the contents on a website as re-showing the contents. I misunderstood and thought you were datamining a site (one-time).
LAN On a local network LAN showing the contents from one server on another should be pretty fast and the websites should perform fairly quickly (by modern standards) even though you're mining content.
You'll want to add the target site's domain and ip to the hosts file so the system doesn't perform a DNS look-up (if caching is disabled) everytime the function is called.
Same box If they were on the same machine you might overload the system if some sort of pauses aren't in place.
WAN If you're on two separate networks in two server farms separated by some distance the number of hops will greatly impact the performance of the script. This is not a good idea in terms of running a live production server for any length of time. Most people will not wait a couple of seconds for a page to load.
Additionally you'll want to filter the mining server's ip out of the stats for the server being mined as it will look like all of your traffic on the target computer comes from one location.
Since they are on two different boxes you'll want to make sure something like IP tables (the firewall) won't lock all access on the target server because you might connect repeatedly to the site too quickly from the same IP. If you're not in control of both networks all sorts of things can also block repeated access such as firewalls and routers. Many web hosts do not like repeated high volumes of traffic. Also you may be penalized for bandwidth if you accidentally misconfigure either box.
Database Replication If you're pulling contents from a database you may want to look into database replication and keep two copies of the database on each machine. Then you would simply load the contents as you would any other file.
回答2:
It depends only on get.php
. If it returns different content on different values of a
, it sure will work.
回答3:
Hopefully I'm understanding you correctly. Your script:
http://wwww.domain1.com/get.php
Some other resource:
http://www.resource.com/other.php
Your get.php, could look like this:
$findResource = $_GET['a'];
// You should perform some validation here on the $findResource value to prevent security issues
$contents=file_get_contents('http://www.resource.com/other.php?a='.$findResource);
回答4:
Yes it's possible, but you have to concern about privacy policy (scraping is not allowed on many sites).
来源:https://stackoverflow.com/questions/16574447/passing-variable-in-a-url-php-file-get-contents