Saving Google Chart image to file using PHP - php

I have a script which generates Google Chart images for clients. It works fine displaying in HTML but I now need to save each chart locally as a .png.
If I try:
$imageData = file_get_contents($url);
file_put_contents('C:/xampp/htdocs/image.png',$imageData);
then I always receive
[function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400
The script works fine for normal images, but not Google charts. Same result if I use cURL to save the image too.
An example of my Google chart $url is below. I've hexed out the colons and pipe symbols but it did not fix the issue:
http://chart.apis.google.com/chart?cht=lc&chxt=x,y&chs=700x400&chco=76A4FB,FF9900&chg=0,8.3333333&chdl=Visits|Unique%20visits&chls=3|3&chma=40,20,20,30&chxr=1,0,33411&chds=0,33411&chd=t%3A33411,33411,33411,33411,33411,33411,33411,33411,33411,33411,33411,33411|33411,33411,33411,33411,33411,33411,33411,33411,33411,33411,33411,33411&chxl=0%3A|Jan-10|Feb-10|Mar-10|Apr-10|May-10|Jun-10|Jul-10|Aug-10|Sep-10|Oct-10|Nov-10|Dec-10

CURL example:
<?php
$ch = curl_init("www.example.com/curl.php?option=test");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output; ?>

I managed to get this to work in the end - it seems the spaces in the label names were causing errors, replacing with %20 did the trick

Related

How Do I Download Facebook Images Using cURL?

I have a PHP script that works just fine downloading most remote images to my file system, but when I try to download a Facebook or Instagram image I get an error that says "failed to open stream: No error in" followed by the line of my fopen function and two additional errors "fwrite() expects parameter 1 to be resource, bool given in" which is obviously due to the Facebook image not downloading.
My code is as follows:
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_BINARYTRANSFER,1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_REFERER, "https://www.facebook.com/");
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)');
$page = curl_exec($curl);
if(curl_errno($curl)):
echo 'Erro: ' . curl_error($curl);
exit;
endif;
curl_close($curl);
// Use basename() function to return the base name of file
$file_name = basename($url);
if(file_exists($file_name)){
unlink($file_name);
}
$fp = fopen($file_name,'x');
fwrite($fp, $page);
fclose($fp);```
There also seems to be an error when I try to add the first line of code to this post which is:
```$url = 'https://scontent-sea1-1.xx.fbcdn.net/v/t1.0-0/p180x540/119041795_176402070632464_6192328410277888324_o.jpg?_nc_cat=111&ccb=2&_nc_sid=825194&_nc_ohc=O7khk9mGFO4AX86nd5X&_nc_ht=scontent-sea1-1.xx&tp=6&oh=ff35f5eaf960fa7bd30ab1d549f0d817&oe=6045A0D1';```
Screenshot APIs are the workaround for this. There are many, but to spare the one I am using from any scrutiny for how it is being used by now I will not name the specific program. Most are basically the same, but you should choose one that allows you to install it on your own system rather than being dependent on simply getting a screenshot from a URL of theirs. That way you can encode the Facebook/Instagram image URL that you want to take a screenshot of, send the encoded URL to the API, and save the result on your file system.
The ability to encode the URL is key because of all the query strings in Facebook/Instagram image URLs.

Playing .m3u8 video using php curl

I'm attempting to play a .m3u8 video using a php curl proxy. The following code seems to work, although it only returns the video #EXTM3U information but does not play the video.
Code:
<?php
//....proxy info
$auth = 'username:password';
$proxy_ip = '1.2.3.4.5';
$proxy_port = 8080;
$path = $_GET['link'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $path);
//curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_port);
curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP');
curl_setopt($ch, CURLOPT_PROXY, $proxy_ip);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $auth);
curl_exec($ch);
if (curl_error($ch)) {
$error_msg = curl_error($ch);
echo $error_msg;
}
curl_close($ch);
if (isset($error_msg)) {
echo $error_msg;
}
?>
Output:
#EXTM3U #EXT-X-VERSION:3 #EXT-X-INDEPENDENT-SEGMENTS #EXT-X-STREAM-INF:BANDWIDTH=5640800,AVERAGE-BANDWIDTH=5640800,CODECS="avc1.4d4028,mp4a.40.2",RESOLUTION=1920x1080,FRAME-RATE=25.000 index4147.m3u8 #EXT-X-STREAM-INF:BANDWIDTH=2421100,AVERAGE-BANDWIDTH=2421100,CODECS="avc1.4d401f,mp4a.40.2",RESOLUTION=1280x720,FRAME-RATE=25.000 index2073.m3u8 #EXT-X-STREAM-INF:BANDWIDTH=1566400,AVERAGE-BANDWIDTH=1566400,CODECS="avc1.4d401f,mp4a.40.2",RESOLUTION=960x540,FRAME-RATE=25.000 index1296.m3u8 #EXT-X-STREAM-INF:BANDWIDTH=1002100,AVERAGE-BANDWIDTH=1002100,CODECS="avc1.77.30,mp4a.40.2",RESOLUTION=746x420,FRAME-RATE=25.000 index783.m3u8 #EXT-X-STREAM-INF:BANDWIDTH=774400,AVERAGE-BANDWIDTH=774400,CODECS="avc1.77.30,mp4a.40.2",RESOLUTION=640x360,FRAME-RATE=25.000 index576.m3u8 #EXT-X-STREAM-INF:BANDWIDTH=421300,AVERAGE-BANDWIDTH=421300,CODECS="avc1.42c015,mp4a.40.2",RESOLUTION=426x240,FRAME-RATE=25.000 index255.m3u8 #EXT-X-STREAM-INF:BANDWIDTH=476300,AVERAGE-BANDWIDTH=476300,CODECS="avc1.42c01f,mp4a.40.2",RESOLUTION=640x360,FRAME-RATE=25.000 index101.m3u8
Any ideas regarding how I can play the video?
Facing the same issue...
Had any luck figuring it out?
I was also facing the same issue, until just a few moments ago.
Was able to generate a m3u8 file in php and set the mime type correctly but videojs and vlc just wouldn't play it...
If I downloaded the output, it would be good for vlc or videojs.
Followed a tip from Markus AO on How to create dynamic m3u8 using php and it worked.
Basically, it should be ignoring the mime type (although I haven't tried removing it) and just looks at the extension.
Just used the htaccess to make it so that when they call some m3u8's it actually calls my php file
Added this to my .htaccess:
RewriteRule ^proxy([^.]+).m3u8$ proxy.php
I'm actually sending the address as a GET parameter such as your $_GET['link']
The wildcard in front of proxy and before the .m3u8 extension is just because I've noticed that a plugin that I've installed in chrome would probably ignore that the parameter changed and just show the same stream as long as it was the proxy.m3u8 file.
Hoping this can be helpful to anybody

php curl return downloaded content to user while file is still downloading

I want to server begins to download a big file. But while this file is downloading output the file content to the user. I tried this code:
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 155000);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch); // get curl response
echo $response;
But this code takes a long time. I want to use curl instead of readfile.
See this answer: Manipulate a string that is 30 million characters long
Modifing the MyStream class should change it enough so that you can just echo the results to the browser. Assuming the browser is already downloading the file, it should just keep downloading it.

How to load an image resource from a URL into a PHP script, using cURL?

I'm using the Google Charts service to generate some QR codes that I afterward need to manipulate (e.g. rotate, scale) in a PHP script and merge with other images to generate one final image.
How do I correctly load such a resource (from a URL) into a PHP script, in a way that will allow me to manipulate it?
An example URL is: https://chart.googleapis.com/chart?cht=qr&chs=500x500&chl=xghsdfgsdfg&choe=UTF-8&chld=L|0
I currently have the following code to retrieve the image using cURL:
function getImage($url){
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$resource = curl_exec($ch);
curl_close ($ch);
return $resource;
}
But when I use it like this:
$image = imagecreatefrompng(getImage("https://chart.googleapis.com/chart?cht=qr&chs=500x500&chl=xghsdfgsdfg&choe=UTF-8&chld=L|0"));
The following error is returned:
Warning: imagecreatefrompng(‰PNG ) [function.imagecreatefrompng]: failed to open stream: No such file or directory in /home/picselbc/public_html/projects/cakemyface/preview.php on line 383
https://chart.googleapis.com/chart?cht=qr&chs=500x500&chl=xghsdfgsdfg&choe=UTF-8&chld=L|0
What you need instead of imagecreatefrompng() is imagecreatefromstring(), because the former expects a filename instead of the file contents itself.
This worked for me
$image = imagecreatefromstring(file_get_contents('http://chart.googleapis.com/chart?cht=qr&chs=500x500&chl=xghsdfgsdfg&choe=UTF-8&chld=L|0'));
header('Content-Type: image/png');
imagepng($image);
Note: I had to use http rather than https because I haven't set up ssl on my local server.

echo + file_get_contents() adds extra line breaks to the output

The following code transfers an image that is created on the fly from a server to a client site using cURL. It stopped working recently and have not been able to find out what the problem is:
// get_image.php
ob_start();
// create a new CURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, 'url/to/image.php');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// set timeouts
set_time_limit(30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
// open a stream for writing
$outFile = fopen($fileDestination, 'wb');
curl_setopt($ch, CURLOPT_FILE, $outFile);
// grab file from URL
curl_exec($ch);
fclose($outFile);
// close CURL resource, and free up system resources
curl_close($ch);
ob_end_clean();
//image.php
/*
* Create image based on client site ...
*/
$filePath = 'path/to/image.png'
$imageFile = file_get_contents($filePath);
header("content-type: image/png");
echo $imageFile;
unlink($filePath);
The file get_image.php is located in a client site and calls the file image.php located in my server.
After running this code the image in the client site is about 7 bytes larger than the original, these bytes seem to be line breaks. After debugging for several hours I found out that these bytes are added when I echo $imageFile. If the 7 bytes are manually removed from the resulting image, the image displays correctly.
There are no errors nor exceptions thrown. The image created in the server is created with no issues. The only output in FF is "The image 'url/to/image.php' cannot be displayed, because it contains errors"
I am not sure what is causing this. Help is greatly appreciated.
Onema
UPDATE:
http://files.droplr.com/files/38059844/V5Jd.Screen%20shot%202011-01-12%20at%2012.17.53%20PM.png
http://files.droplr.com/files/38059844/QU4Z.Screen%20shot%202011-01-12%20at%2012.23.37%20PM.png
Some things to check.
That both files are stored without BOMs
That '<?php' are the first five characters and '?>' the last two in both files.
That when you remove the ob_start() and ob_end-clean(() it should show no error messages.
If you put the unlink before the genereation, you can see the genereated file - check it is valid.
You might want to start the practice of leaving the final ?> from the end of your files - it isn't necessary, and can cause problems if there is whitespace and newlines following the php delimiter.

Categories