 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
12-03-2007, 03:59 PM
|
#1 (permalink)
|
|
The Acquainted
Join Date: Nov 2007
Posts: 127
Thanks: 14
|
Using curl to read a file from an external server?
So I have to read a pdf file as an attachment from an external server. I use for the local server:
PHP Code:
curl_setopt($ch, CURLOPT_URL, 'server'); curl_setopt($ch, CURLOPT_USERPWD, "u:p"); curl_setopt($ch, CURLOPT_POSTFIELDS, 'pdfs=' . $_GET['pdfs']); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
And on the external server with the actual pdf:
PHP Code:
header('Content-Type: application/pdf'); header('Content-Disposition: attachment; filename=test.pdf'); readfile('somepdfgeneratedbytheserver.pdf');
When I use curl_getinfo();, it does return:
Code:
[content_type] => application/pdf
The problem with the code above is that when I echo the results, it spews out all this garbage text.
Any ideas?
|
|
|
|
12-03-2007, 04:08 PM
|
#2 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
You'll have to set the header in your PHP document so that once you've acquired the PDF, it can be treated like a PDF. So before you echo out the PDF's content, add:
php Code:
header('Content-Type: application/pdf');
In fact, remove the above line from the PDF side of things, and move it across to the PHP side where you're using cURL.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
|
The Following User Says Thank You to Wildhoney For This Useful Post:
|
|
12-03-2007, 04:17 PM
|
#3 (permalink)
|
|
The Acquainted
Join Date: Nov 2007
Posts: 127
Thanks: 14
|
Very nice dude, thanks a bunch. :)
|
|
|
|
12-03-2007, 04:37 PM
|
#4 (permalink)
|
|
The Frequenter
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
|
To my knowledge, header should be called upon before there is ANY output what so ever? HTML or print/echo? By the way gcbdm, nice script.
|
|
|
12-03-2007, 04:39 PM
|
#5 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
Yea, definitely. You're spot on. Before ANY output else you will receive a fairly nasty error. Just add the header function before the echo construct.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
12-03-2007, 04:47 PM
|
#6 (permalink)
|
|
The Frequenter
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
|
Then I've got a question for ya Wildhoney. Yesterday I saw a script, calling up header in a switch, midpage, with outputs all around. He used sessions & cookies, with my (basically yours) salt script, enhanced IP (browser, lang, windows & IP combined) and a bit more (with the lardge encoding sha1+md5.
Anyways, what's the big deal about header? I really don't get it, but still, I know how to use it. Header can define the page properties but you can use it as a redirect as well. (ofcourse after the header(''); you call up exit(); (with, if you want, a message between the ().
So, can you explain this to me? :D
|
|
|
12-03-2007, 04:57 PM
|
#7 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Quote:
Originally Posted by Wildhoney
In fact, remove the above line from the PDF side of things...
|
Erm, why? 
|
|
|
|
12-03-2007, 05:13 PM
|
#8 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
Quote:
Originally Posted by Salathe
Erm, why? 
|
Don't have to, but if the PDF side is not used for direct viewing, then it's going to return the PDF data no matter what. It seems the viewing is done on the cURL side of things as opposed to the PDF side of things. So if that's the case then it's not a requisite that you set the content type on there, only on the page where the PDF is getting interpreted as a PDF.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
12-03-2007, 05:19 PM
|
#9 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
Quote:
Originally Posted by ReSpawN
Then I've got a question for ya Wildhoney. Yesterday I saw a script, calling up header in a switch, midpage, with outputs all around. He used sessions & cookies, with my (basically yours) salt script, enhanced IP (browser, lang, windows & IP combined) and a bit more (with the lardge encoding sha1+md5.
Anyways, what's the big deal about header? I really don't get it, but still, I know how to use it. Header can define the page properties but you can use it as a redirect as well. (ofcourse after the header(''); you call up exit(); (with, if you want, a message between the ().
So, can you explain this to me? :D
|
Well, there's no way he would have got away with that. Basically the header function is used to transmit header information - that is, header information that is interpreted by the browser, but not necessarily displayed. The packet that comes after are the HTML pages and all the images - all coming from simple GET/POST requests.
So, if you send the body before you issue the header function then because the header function has already been dispatched, you cannot send another header function once the first header has gone. Thus:
Send: HTML GET/POST Request -> Receive: HTML Header -> Receive: HTML Body.
After the HTML body comes nothing, but more body stuff. With the way HTTP works you cannot set another header function - that is, those items in <head>.
To get around this problem you can prevent the body stuff being sent prematurely by using such functions as as the ob_* library. This stores the body in an internal buffer until you tell it to send that buffer to the client. Therefore it allows you to send all the header stuff, still interpret the body as well, but send them in order: header then body.
The best way to go about it though is to not use the ob_* library at all and simply create your PHP script so that all PHP is processed before any output is displayed, then just mingle the PHP echoes amongst the HTML.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
12-03-2007, 05:43 PM
|
#10 (permalink)
|
|
The Frequenter
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
|
Well THAT sure it a lot to take in all at once, but I knew I was right. Header can only be called upon before any output. That is fairly enough to know if you're getting into advanced programming. Well, if you ask me.
I'm going to do some research into the ob_ library you just talked about. Thanks for the explanation Wildhoney!
|
|
|
12-04-2007, 12:46 AM
|
#11 (permalink)
|
|
The Contributor
Join Date: Nov 2007
Location: California
Posts: 82
Thanks: 0
|
Here's a little snippet that usually explains an easy way to suck remote data rather easily.
PHP Code:
<?php
$ch = curl_init("http://www.mywebserver.com/path/to/file");
// give it a fake uer agent curl_setopt($ch,CURLOPT_USERAGENT,"Interweb Explorer");
// start the buff ob_start();
curl_exec($ch); curl_close($ch);
$my_output = ob_get_contents();
ob_end_clean;
?>
Also note, that the ob_ function set can be used to make your web pages appear faster on certain browsers.
For example, if you start ob_start(), and beging flushing out the header and body as it comes, the body won't have to wait for certain block level objects like <TABLE> tags to load fully before they start to show up.
Just a little trick you can apply every now and then..
--
Note: was just using the simple curl and ob_ functions to show what they can do together, it's in no way a complete snippet (though it does work).
Last edited by dschreck : 12-04-2007 at 12:48 AM.
Reason: added a note.
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|