Displaying content from a .txt file using a cURL - php

I am trying to display content from a txt file on a remote server using a cURL if the function file_get_contents is not turned on on their server. Right now, I have it set up as:
$updatelog ='http://www.linktoremotsite.com/updatelog.txt';
if(function_exists('file_get_contents')){
$log = file_get_contents($updatelog);
}else{
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $updatelog);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$log = curl_exec($ch);
curl_close($ch);
}
echo '<div>' . $log . '</div>';
and the file_get_contents works fine, but for those that don't have that enabled on their site, I like them to be able to use a curl to see the contents of the .txt file. But I can't seem to get the contents of the .txt file to display.
The .txt file does have some HTML in it for the file_get_contents to display neatly, but not much and I like to keep that with the curl if possible.

Related

Display image from curl request in html<img> tag

One of the pages of a website i'm working on should display information about a manga such as it's cover image.
I'm trying to display an image I got by making a curl request.
<?php
if(isset($_GET['info'])) {
$postedData = $_GET["info"]; //json object containing info about manga such as author/title etc.
$info = json_decode($postedData, true);
$mangacoverid = $info['im']; //id of the cover image that i'm getting from json object
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://cdn.mangaeden.com/mangasimg/" . $mangacoverid); //link of API i'm using and adding cover id to it
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$picture = curl_exec($ch);
curl_close($ch);
header('Content-type: image/jpeg');
echo $picture; //displays image in browser
}
?>
-Some 'mangacoverid' for testing purposes:
ff/ff94bb880357b6b811bccbbfd3356c5ec41fbb184291323f0ed6a86a.jpg
c1/c1b0173d8986681f23ecf5a69d26aa9dab4a04db4d40f99bed539198.jpg
0c/0cf6ebf78074e748ab2aeea3a0fcb9e0dd43040974c66e24fa46703f.jpg
5d/5dcfed2e033c2da62e7ac89367533ebbc344373c46a005e274a16785.png
18/18b1f0b13bccb594c6daf296c1f9b6dbd83783bb5ae63fe1716c9091.jpg
35/35bf6095212da882f5d2122fd547800ed993c58956ec39b5a2d52ad4.jpg
-While I am able to display the image in the page, the whole page background becomes black with the image in the middle of the page. (i.e. style="margin: 0px; background: #0e0e0e;>").
What I am trying to do is to insert the image in an HTML tag, so that I can place it somewhere else in the page.
I tried just putting the normal cdn link with 'mangacoverid' attached to it in an tag but the image provider doesn't allow hotlinking and it throws 403 error.
Any help really appreciated!
I tested your code and it seems to be working correctly when the cover ID is hard coded.
Could the issue be that the JSON passed via query param is not getting parsed correctly (due to URL encoding)?
Does it work correctly for you with a cover ID hard coded?
File image.php
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, sprintf('https://cdn.mangaeden.com/mangasimg/%s', $_GET['id']));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$picture = curl_exec($ch);
curl_close($ch);
header('Content-type: image/jpeg');
echo $picture;
And in your script that generates the page displaying the image:
// Assuming $info holds the decoded json for the manga
echo(sprintf('<img src="image.php?id=%s>', $info['im']);
Ok, I'll post the solution in case anyone ever bumps into this.
The code is the mostly the same as original: just edited a few lines as shown below.
-I created a file in the directory called 'tempcover.jpg'.
-I got the image using the function 'imagecreatefromstring($picture)'.
-I saved the image into the file with 'imagejpeg($img,'tempcover.jpg', 100)'.
By doing this I can just refer to the file in the HTML tag and display it the way I want to.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://cdn.mangaeden.com/mangasimg/" . $mangacoverid); //link of API i'm using and adding cover id to it
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$picture = curl_exec($ch);
curl_close($ch);
$img = imagecreatefromstring($picture);
imagejpeg($img,'tempcover.jpg', 100);
?>
<!--HTML-->
<img src="tempcover.jpg" alt="bruh" height="400px" width="300px">

Collecting file with PHP CURL after validating request downloads an empty file

I am doing a system where one of my sites goes to the other to get documents.
On the first site I am using Curl to make a request to get the file wanted:
I am using the solution from Download file from URL using CURL :
function collect_file($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, false);
curl_setopt($ch, CURLOPT_REFERER, "http://example.com");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
return($result);
}
function write_to_file($text,$new_filename){
$fp = fopen($new_filename, 'w');
fwrite($fp, $text);
fclose($fp);
}
$curlUrl = 'http://site2.com/file-depository/14R4NP8JkoIHwIyjnexSUmyJibdpHs5ZpFs3NLFCxcs54kNhHj';
$new_file_name = "testfile-new.png";
$temp_file_contents = collect_file($curlUrl);
write_to_file($temp_file_contents,$new_file_name);
I am testing downloading an image. If i use a direct URL into $curlUrl , for instance http://site2.com/file-depository/image.png it works perfect.
What I am doing is that the URL http://site2.com/file-depository/14R4NP8JkoIHwIyjnexSUmyJibdpHs5ZpFs3NLFCxcs54kNhHj is then parsed and checked against a database to match the document requested, once there is a document matched I need to provide this document to the Curl response.
I have tried many ways to read the file but everytime i am getting a file on the other end but it is only 1kb in size (45 expected) and when trying to open it i get an error unkown file type etc.
On the second site, once the URL is validated here is what I have:
$file = readfile('some-image.png');
echo $file;
I am guessing there is part of the information which belongs to the file missing but can't figure it out, any pointers appreciated!
I have replaced
function write_to_file($text,$new_filename){
$fp = fopen($new_filename, 'w');
fwrite($fp, $text);
fclose($fp);
}
by file_put_contents($new_file_name,trim($temp_file_contents));
Please note the trim(), the issue was that I was apparently collecting some empty space in front of the file content.

Streaming video from one server thru another

I need a help with my script, I spend ages working out what is wrong with it, but I can't find anything.
Basically on my first server I contain video files, on my second server I stream those files. To get the files from the first server you have to use IP from the server two (Basically I allow just my servers to connect with the first server)
So to get those files I use cURL function because all video will pass server two.
I did this :
$url = "111.111.111.111/example.flv";
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$video = curl_exec($ch);
curl_close($ch);
// output to browser
header("Content-type: video/x-flv");
echo ' <a href="'.$video.'" style="display:block;width:920px;height:600px" id="player">
</a>
<script>
flowplayer("player", "flowplayer-3.2.16.swf");
</script>';
So what I did is :
1. I have a link to the video in $url
2. I open new connection using cURL
3. I take data from the link.
4. Then I keep this video in $video
5. And I try to open it.
But but it doesn't work... When I open this website is show me this :
FLV��� �����ÖY��������
onMetaData����metadatacreator�3Yet Another Metadata Injector for FLV - Version 1.8�hasKeyframes�hasVideo�hasAudio�hasMetadata�canSeekToEnd�duration�#Ļj◊
=p§�datasize�AĽ≥i���� videosize�A∂ąęZ���� framerate�#9�70Ō'�
videodatarate�#|<GłÕŰĢ�videocodecid�#�������width�#Ą�������height�#v#������ audiosize�AĒ8»,����
audiodatarate�#Xsöł¶#ű�audiocodecid�#$�������audiosamplerate�#�������audiosamplesize�?ū�������stereo�filesize�AĽ≥Óv����
lasttimestamp�#Ļj◊
=p§�lastkeyframetimestamp�#Ļj◊
=p§�lastkeyframelocation�AĽ≥Ób���� keyframes�
filepositions
So I get data in string...
Do you know what I have to do, to stream this file ??
You can't output both the HTML and the FLV data at the same time. I'd recommend creating a separate PHP file with the following code:
$url = "111.111.111.111/example.flv";
header("Content-type: video/x-flv");
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_exec($ch);
curl_close($ch);
Then you can put your player code anywhere else, like:
<a href="/path/to/first/file.php" style="display:block;width:920px;height:600px" id="player">

Getting xml file through command-line php script

How do I get the content of an xml file through a command line php script? If I access this link through the IE browser, I am able to get the XML file: http://alerts.weather.gov/cap/ma.php?x=0. If I try to get the file through command line with c:\path\php.exe get_advisory_upd.php, the script shows an error host did not respond in allowed time. This seems to be a security issue. I must have a scheduled task to get that xml at specified intervals. How do I do that? file_get_contents() returned that same error, simplexml_load_file() might not have shown any errors, but did not get the xml file.
PHP Script get_advisory_upd.php:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$file = 'atom-advisory-MAZ015_UPD.txt';
$newfile = 'atom-advisory-MAZ015.txt.bak';
if (!copy($file, $newfile)) {
echo "Failed to copy $file...\n";
}
/* $contents = file_get_contents('http://alerts.weather.gov/cap/ma.php?x=0'); */
// Use cURL to get the RSS feed into a PHP string variable.
$ref_url = "http://192.x.x.x/weather/get_advisory_upd.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://alerts.weather.gov/cap/ma.php?x=0');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_REFERER, $ref_url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1000);
$contents = curl_exec($ch);
echo 'Curl error: '. curl_error($ch);
curl_close($ch);
/* $contents = simplexml_load_file('http://alerts.weather.gov/cap/ma.php?x=0');
echo "contents \n".$contents; */
file_put_contents($file, $contents);
?>
UPDATE
I am running this script from an intranet. As y_a_v_a suggested I specified the CURLOPT_REFERER option to tell the remote host my url. I do that with
$ref_url = "http://192.x.x.x/weather/get_advisory_upd.php";
curl_setopt($ch, CURLOPT_REFERER, $ref_url);
Is there a different way to specify the URL?
Set a sane CURLOPT_REFERER and set CURLOPT_CONNECTTIMEOUT to zero and run your script. Verify what happens by adding
$curl_error = curl_error($ch);
and dump $curl_error after you closed the curl action.

Updating Twitter background via API

I'm having a little trouble updating backgrounds via Twitter's API.
$target_url = "http://www.google.com/logos/11th_birthday.gif";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$html = curl_exec($ch);
$content = $to->OAuthRequest('http://twitter.com/account/update_profile_background_image.xml', array('profile_background_image_url' => $html), 'POST');
When I try to pull the raw data via cURL or file_get_contents, I get this...
Expectation Failed The expectation given in the Expect request-header
field could not be met by this server.
The client sent
Expect: 100-continue but we only allow the 100-continue expectation.
OK, you can't direct Twitter to a URL, it won't accept that. Looking around a bit I've found that the best way is to download the image to the local server and then pass that over to Twitter almost like a form upload.
Try the following code, and let me know what you get.
// The URL from an external (or internal) server we want to grab
$url = 'http://www.google.com/logos/11th_birthday.gif';
// We need to grab the file name of this, unless you want to create your own
$filename = basename($url);
// This is where we'll be saving our new file to. Replace LOCALPATH with the path you would like to save the file to, i.e. www/home/content/my_directory/
$newfilename = 'LOCALPATH' . $filename;
// Copy it over, PHP will handle the overheads.
copy($url, $newfilename);
// Now it's OAuth time... fingers crossed!
$content = $to->OAuthRequest('http://twitter.com/account/update_profile_background_image.xml', array('profile_background_image_url' => $newfilename), 'POST');
// Echo something so you know it went through
print "done";
Well, given the error message, it sounds like you should load the URL's contents yourself, and post the data directly. Have you tried that?

Categories