file_get_contents sometimes does not find texts - php

I'am new to PHP, so please be nice :)
Sometimes file_get_contents does its job and sometimes not. I have build a simple check for URLs, if they exist, on webpages. But the problem is, that even the URL exist for sure (manually checked the sourcecode), file_get_contents and preg_match do not find it. I can't figure out why. Here's the code:
$page = $URL_that_should_be_checked;
$checkurl = str_replace("/", "\/", $checkurl);
$page = file_get_contents($userpage);
$checkurl = "/".$checkurl."/";
$report = preg_match($checkurl, $page);
Thank you very much!!

Try this:
$page = file_get_contents($userpage);
$report = preg_match('/' . preg_quote($checkurl) . '/', $page);

It's rather difficult to follow your code.
Did you check the documentation page and note that special characters may need to be encoded with the urlencode() function?
On this line $checkurl = str_replace("/", "\/", $checkurl); the variable $checkurl does not appear to have a definition.
On this line $userpage does not seem to be defined. Only $page is defined in the code you've provided.
It looks like you're doing a lot of work to set up a preg_match and $report its value. It's unclear why you need to fetch the page during this process.
Also, do you have allow_url_fopen set to true? Are you getting any error messages?

You should use the PHP Function preg_quote to parse the URL to your preg_match Function. Example:
$checkurl = preg_quote($checkurl);
See: http://php.net/manual/de/function.preg-quote.php

Not sure what your code is doing.
You do
$page = $URL_that_should_be_checked;
but two lines later do
$page = file_get_contents($userpage);
without ever having set $userpage.

Related

PHP - How to see if a word is on a specific webpage?

I need to check if a webpage outside of my site has a specific word on it. I’ve tried file_get_contents() but it doesn’t return anything. Is there any way I can do this in PHP?
edit: Here’s what I’ve tried:
$query = 'example';
$file = "https:// www.site.com/search?q=$query";
// tested url and it works, had to add space to post it
$contents = file_get_contents($file);
echo $contents;
I was expecting it to just output the entire page for me to use .includes() on later but it just doesn’t output anything.
Look into curl to get the contents of a web page. Then you can use preg_match to find the word.

“&” being replaced by "& in php include

I have a php page which get response from another page as shown:
while($response!=200)
{
$response = include 'xyz.php?one='.$one.'&two='.$two.'&three='.$three.'';
}
But my link always get's something like:
domainname.com/xyz.php?one=content&two=content&three=content
And due to & getting replaced by & I am getting the page not found issue.
I have tried using %26 and directly putting & instead of &, all in vain.
Is there any other simple solution besides using string replace function of PHP to remove & and replace it with &
Check out html_entity_decode
$response = html_entity_decode($response)
I ran a test based on the code you sent and I don't have a problem. That suggests you have something auto-magical going on in your *.ini file (magic quotes, maybe... ugh...). Try to create the string simply as a variable to remove it from the filename context and echo it out to be sure it's right, then use the variable with your include.
$one = 'abc';
$two = 'def';
$three = "ghi";
$file= 'xyz.php?one='.$one.'&two='.$two.'&three='.$three;
echo "\n\n".$file;
$response = include $file;
You can't use URL parameters when accessing a local file, they have to go through the webserver. Try:
$response = file_get_contents("http://localhost/path/to/xyz.php?one='.$one.'&two='.$two.'&three='.$three);

$_GET, Ampersand is interrupting

Trying to use a $_GET['url'] variable to grab data from a URL:
http://mysite.com/?url=http://this.is/?q=an&?example=url
What I want above is bolded, but sadly the $_GET['url'] will only get "http:// this.is/?q=an" because the & makes it interpret it as the beginning of a new variable within the URL.
Is there a way to ignore the ampersands so my script can get the entire URL I need it to? The URL that is appended to ?url= is not within my limits to control so most work but some do contain the dreaded &. After reading questions on Stack Overflow I'm not holding out much hope :(
If you have absolutely no control over the arguments placed on the query string (for whatever reason), you can also do this by manually parsing the $_SERVER['QUERY_STRING'] varible, e.g.
$page = str_replace("url=", "", $_SERVER['QUERY_STRING']);
Of course, if possible, you should encode it using the answers posted by everyone else.
Use urlencode()
$get_url = urlencode('http://this.is/?q=an&?example=url');
$url = 'http://mysite.com/?url=' . $get_url;
If you can't control the query string, you could use
$query = $_SERVER['QUERY_STRING'];
$pos = strpos($query, "url=");
if ($pos !== false) {
$url = substr($query, $pos + 4);
}
This code returns everthing after url=
If you have the possibility,you should encode the url with urlencode to create a clean url.
If you don't do this, you get eventually an server error, with strange urls and you can't pass more than one url(because everything after the url= is interpreted as url)
Here is the tested code:
if (!empty($_GET['url'])) {
echo $_GET['url'].'<br />';
}
$url = urlencode('http://this.is/?q=an&?example=url');
echo 'LINK';
Hope it will help you.

Correct syntax for php inside a feed request

I have a very basic query string which passes a ID to a receiving page.
On that page, I need to dynamically call the YouTube API, giving my playlistID.
I'm having to use PHP for this, and it's a little out of my comfort zone, so hopefully someone can wade in with a quick fix for me.
Here is my variable
$playlist;
And I need to replace the 77DC230FBBCE4D58 below with that variable.
$feedURL = 'http://gdata.youtube.com/feeds/api/playlists/77DC230FBBCE4D58?v=2';
Any help, as always, greatly appreciated!
Once the $playlist variable is set you can construct the feed URL as :
$feedURL = 'http://gdata.youtube.com/feeds/api/playlists/' . $playlist . '?v=2';
or
$feedURL = "http://gdata.youtube.com/feeds/api/playlists/$playlist?v=2";
$feedURL = 'http://gdata.youtube.com/feeds/api/playlists/'.rawurlencode($playlist).'?v=2';
Or perhaps a little neater:
$feedurl = sprintf(
'http://gdata.youtube.com/feeds/api/playlists/%s?v=2',
rawurlencode($playlist)
);
(Note: rawurlencode is used just in case [not that it's likely with YouTube playlist IDs] the $playlist value contains any funky characters.)
More infos:
String concatenation with the . operator
Encoding potentially "unsafe" URL characters with rawurlencode
Adding values to "formatted strings" with sprintf
$feedURL = 'http://gdata.youtube.com/feeds/api/playlists/' . $playlist . '?v=2';
you use a full stop to join strings in PHP

str_replace and preg_replace work on one server but not another

UPDATE: As it turns out, the below is caused by a caching issue on my production server. Thanks to everybody who contributed thoughtful answers.
I have a simple function on a php page that takes a url such as:
http://myurl.com/mypage.html?param1=value1
and converts it to:
http://myurl.com/searchpage.html?param1=value1
All it does it swap out the page.html portion.
To do this, I use the following:
$currentUrl = $this->getCurrentUrl(); // Grabs the current url, i.e 'http://myurl.com/mypage.html?param1=value1'
// Derive a search pattern from the current url
$pattern = "/" . str_replace(array("/", ".", "-"), array("\\/", "\\.", "\\-"), $currentUrl) . "/";
// get rid of the 'mypage.html'
$newUrl = preg_replace($pattern, 'http://myurl.com/', $currentUrl);
// replace the question mark with the correct page
$newUrl = str_replace("/?", "/searchpage.html?", $newUrl);
The above code is not the exact code but is a good representation. It works beautifully on one server, but when I push to production, the preg_replace does not work. I originally attempted to use str_replace. It also works on my local development machine, but not on the production server.
I have confirmed that the URL variables are coming in correctly. Any ideas?
That's horribly convoluted (sorry to say). Why not just:
$newUrl = preg_replace('!\bmypage\.html\b!', 'searchpage.html', $oldUrl);
Why don't you just do
$pieces = explode('/',$url);
str_replace('mypage','searchpage',$pieces[2]);
$newURL = implode('/',$pieces);
Way better than using regexps.

Categories