TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Using curl to read a file from an external server? (http://www.talkphp.com/advanced-php-programming/1582-using-curl-read-file-external-server.html)

bdm 12-03-2007 03:59 PM

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?

Wildhoney 12-03-2007 04:08 PM

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.

bdm 12-03-2007 04:17 PM

Very nice dude, thanks a bunch. :)

ReSpawN 12-03-2007 04:37 PM

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.

Wildhoney 12-03-2007 04:39 PM

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.

ReSpawN 12-03-2007 04:47 PM

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

Salathe 12-03-2007 04:57 PM

Quote:

Originally Posted by Wildhoney (Post 5125)
In fact, remove the above line from the PDF side of things...

Erm, why? :-/

Wildhoney 12-03-2007 05:13 PM

Quote:

Originally Posted by Salathe (Post 5142)
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.

Wildhoney 12-03-2007 05:19 PM

Quote:

Originally Posted by ReSpawN (Post 5141)
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.

ReSpawN 12-03-2007 05:43 PM

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!

dschreck 12-04-2007 12:46 AM

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).


All times are GMT. The time now is 09:33 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0