TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-03-2007, 03:59 PM   #1 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default 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($chCURLOPT_URL'server');
    
curl_setopt($chCURLOPT_USERPWD"u:p");
    
curl_setopt($chCURLOPT_POSTFIELDS'pdfs=' $_GET['pdfs']);
    
curl_setopt($chCURLOPT_HEADER1); 
    
curl_setopt($chCURLOPT_RETURNTRANSFER1); 
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?
bdm is offline  
Reply With Quote
Old 12-03-2007, 04:08 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
bdm (12-03-2007)
Old 12-03-2007, 04:17 PM   #3 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default

Very nice dude, thanks a bunch. :)
bdm is offline  
Reply With Quote
Old 12-03-2007, 04:37 PM   #4 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

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.
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 12-03-2007, 04:39 PM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-03-2007, 04:47 PM   #6 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

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
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 12-03-2007, 04:57 PM   #7 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
In fact, remove the above line from the PDF side of things...
Erm, why?
Salathe is offline  
Reply With Quote
Old 12-03-2007, 05:13 PM   #8 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
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.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-03-2007, 05:19 PM   #9 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Quote:
Originally Posted by ReSpawN View Post
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.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-03-2007, 05:43 PM   #10 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

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!
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 12-04-2007, 12:46 AM   #11 (permalink)
The Contributor
 
dschreck's Avatar
 
Join Date: Nov 2007
Location: California
Posts: 82
Thanks: 0
dschreck is on a distinguished road
Default

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.
dschreck is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 01:34 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design