file_get_contents don't work with some images - php

I am trying to download an image with file_get_contents PHP's function.
It receive an urlencode(url) via GET and return the content.
This is the code:
<?php
$url=($_GET["url"]);
$url2 = ("http://www.liberoquotidiano.it/resizer.jsp?w=500&h=-1&maximize=true&img=upload/cut1372677360319.jpg&filetype=image.jpg");
echo "<br>Url 1 is via GET <br> Url2 is a variable instantiated in the script and its value is manually inserted.";
echo "<br>file_get_contents Url2 work, but with url1 not,althought the url content is the same. ";
echo "<br>1.url= ".$url;
echo "<br>2.url= ".$url2;
$r=strcmp($url2,$url);
if($r==0){
echo "correct";
}else{
echo "<br><br>string compare with url and url2 return ".$r;
}
echo "<br><br>launch: file_get_contents(url) => ";
$image_data = file_get_contents($url);
echo $image_data;
?>
url and url2 are the same but php strcmp code return 1, and not 0...I don't understand why.
If I launch
file_get_contents($url);
it don't work and I haven't returned any value.
If I launch
file_get_contents($url2);
it correctly work.
The curiousness is that url and url2 contains the same value, but the results differ.
This is the link at the script:
http://www.clouderize.it/michele/get_cont.php?url=http%3A%2F%2Fwww.liberoquotidiano.it%2Fresizer.jsp%3Fw%3D500%26amp%3Bh%3D-1%26amp%3Bmaximize%3Dtrue%26amp%3Bimg%3Dupload%2Fcut1366795446185.jpg%26amp%3Bfiletype%3Dimage.jpg
What may be the problem?
Thank you a lot.

$url = $_GET['url];
Are you using any html sort of things in the form from which you are fetching url. Is it there that user is entering url with html tags too or you are including some html tags with the url. Because, if strcmp is not giving 0 as output, it means that both url strings are not equal. There must be something which is causing the issue. And it can be html tags. Just check it once.

Oks the problem is that in url there are some & I have substituted these with &.
$src=str_replace("&", "&", $src);

Related

Fetching specific URL with preg_match

This is my code :
$patt = "#href=\"(.*?)\"#";
preg_match($patt,$data,$match);
echo $match[1];`
i.e. theres a URL in the HTML code of the page $data
<a href="http://aba.ai/iEU9x">
I want to grab this link above. Thanks

How to fetching data with php from a website and put its data to variable?

I am going to read a website data and put its data in a variable.
$site=file_get_contents($url);
I am going to work with $site variable and read data from $url:
for example I am going to fetch data from this url
http://en.wikipedia.org/wiki/List_of_states_and_territories_of_the_United_States
How Should I do it?
I used explode function but I want to get center of a div for example when there is a div I need to get what is in div
<div id="div1">
...I need to get here between open and close tag of div
</div>
Using regular expression:
<?php
$url = "http://somesite.com/";
$begining_match = '<body>';
$end_match = '</body>';
$file_contents = file_get_contents($url);
$extracted_contens = preg_replace("/\$begining_match([a-z0-9\.])\$end_match/i", "\\1", $file_contents);
echo $extracted_contents;
?>
Taken from: http://www.webdeveloper.com/forum/showthread.php?37464-possible-to-have-file_get_contents-only-grab-between-two-things

php look for string in address bar, replace body contents accordingly

I am looking for the php "header" code and in-html-body-phpsnippet which will look for an exact string such as "redwidget" or "bluewidget" "greenwidget" etc in the address bar, and modify the output of the in-body <phpsnippet> based on which of those strings is there in the address bar
... in other words, I can define a list of parameters to look for in the address bar, and the <phpsnippet> will modify the in-body html output based on which it finds
ALSO if it finds none of them, there is a default or "fallback" output
What would the header script and also the in-body be?
My goal is to change the image displayed on a page based on which parameter it finds in the address bar
***Begin Example listing of outputs:
exact string "redwidgets" found in address bar -> <phpsnippet> outputs "/images/redwidget.jpg"
or exact string "greengoblin" found in address bar -> <phpsnippet> outputs "/images/greengoblin.jpg"
or exact string "bluewidgets" found in address bar -> <phpsnippet> outputs "/images/bluesteel.jpg"
or None of the above exact strings found in address bar -<phpsnippet> outputs "/images/defaultplaceholderimagethingy.jpg"
***End example
Thanks in advance!
<?php
$validKeywords = array("a", "b");
//ternary if/else: condition ? this if true : otherwise this;
$keyword = isset($_GET['keyword']) && in_array($_GET['keyword'], $validKeywords) ? $_GET['keyword'] : "defaultplaceholderimagethingy";
//format the output with sprintf and store it for later use
$output = sprintf("<img src='images/%s' alt='%s' />", $keyword.".jpg", "descriptive text");
print $output;
?>
Hope this helps.
Why don't you use GET parameters?
http://myurl/?phpsnippet=redwidget
In your code:
if (isset($_GET['phpsnippet']) {
// use $_GET['phpsnippet'] which, in this case, equals 'redwidget'
} else {
// display default
}
Ok I ALMOST figured this one out...
Here is my header code:
if (isset($_GET['keyword'])) {
$keyword = $_GET['keyword'];
}
And my in-body html/php which replaces the image based on the keyword parameter:
<img alt="" src="http://takeherhometonight.net/wp-content/uploads/<?php echo $keyword; ?>.jpg" border="0" width="300" height="314" />
The only thing I can't get with the above is a DEFAULT image, should a keyword parameter not be present.... any ideas?

How to get a url from a database

So I have three pages one that is the index page. One that writes the data from a form inside the index page to the database. And one that gets data from the database and echos out a html table with the data inside.
Currently if you write a link in the form. It will just come out as text. I would like the whole link to be like [link].
so say if I wrote this onto the form:
Look at this: www.google.com or Look at this: https://www.google.com
it would come out like this in html
Look at this: www.google.com
How could I go about doing this?
Okay so the html is:
<form class="wide" action="Write-to.php" method="post">
<input class="wide" autocomplete="off" name="txt" type="text" id="usermsg" style="font-size:2.4vw;" value="" />
</form>
in which the user would write:
"Look at this: www.google.com or Look at this: https://www.google.com"
This would then get sent to the database through Write-to.php.
$sql="INSERT INTO social (comunicate)
VALUES
('$_POST[txt]')";
}
this then gets written back into the database:
$result = mysqli_query($con,"(select * from social order by id desc limit {$limit_amt}) order by id asc");
while($row = mysqli_fetch_array($result))
{
echo "<tr div id='".$i."' class='border_bottom'>";
echo "<th scope='col'>";
echo "<span class='text'>".htmlspecialchars($row['comunicate'])."</span><br />";
echo "</th>";
echo "</tr>";
}
Just try:
echo(''.$your_url_variable.'');
Update:
The OP really wanted to detect url's in a string. One possible solution could be filter it using a regular expression. This code could help:
<?php
// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
// The Text you want to filter for urls
$text = "The text you want to filter goes here. http://google.com";
// Check if there is a url in the text
if(preg_match($reg_exUrl, $text, $url)) {
// make the urls hyper links
echo preg_replace($reg_exUrl, "{$url[0]} ", $text);
} else {
// if no urls in the text just return the text
echo $text;
}
?>
Source: http://css-tricks.com/snippets/php/find-urls-in-text-make-links/
There are quite a few things you need to worry about when displaying user supplied (tainted) data.
You must ensure that all the data is sanitised -- never ever just echo the content, look into htmspecialchars and FILTER_VALIDATE_URL for example:
function validateUrl($url) {
return filter_var($url, FILTER_VALIDATE_URL);
}
What you are attempting to do is convert a string into a link, for example you can write a function like this:
function makeClickable($link) {
$link = htmlspecialchars($link);
return sprintf('%s', $link, $link);
}
You can use string concatenation as well, but I wouldn't do that in my view code. Just personal preference.
Take a look at the urlencode function, it will certainly come in handy.
I would also recommend you read about cross site scripting
Please note that I am not making any implementation recommendations, my aim is just to show you some contrived code samples that demonstrate making a string "clickable".
Update:
If you would like to make links clickable within text, refer to the following questions:
Best way to make links clickable in block of text
Replace URLs in text with HTML links
save the hyperlink in db and retrieve as a string by sql query
like:
select link from table_name where index = i
and save link as: whaatever here
and print it
Use this
echo '' . $res['url'] . '';

Get .mp4 source and poster image from Vine Id (PHP)

How to get video.mp4 from vine url?
Example:
from https://vine.co/v/hnVVW2uQ1Z9
I need http://.../*.mp4 and http://.../*.jpg
Script what I need use this page vinebed.com
(In PHP)
Thanks much.
It's very simple. if you check the source of a vine video from vine.co you'll see the meta tags. and you should see twitter:player:stream. By using php you can extract that information specifically and use it like a variable.
<?php
function vine( $id )
{
$vine = file_get_contents("http://vine.co/v/{$id}");
preg_match('/property="twitter:player:stream" content="(.*?)"/', $vine, $matches);
$url = $_SERVER['REQUEST_URI'];
return ($matches[1]) ? $matches[1] : false;
}
?>
And to set an $id you will need to create a function that will either A) Automatically read a vine video id by url and you can display it like this <?php echo vine('bv5ZeQjY35'); ?> or B) Just set a vine video id and display as is.
Hope this helps as it's worked for me just fine.

Categories