How to read/to download attachment on an URI? [message #172491] |
Mon, 21 February 2011 10:59 |
n00m
Messages: 25 Registered: February 2011
Karma: 0
|
Junior Member |
|
|
$contents = stream_get_meta_data($handle);
=================================================
Array
(
[wrapper_data] => Array
(
[0] => HTTP/1.1 200 OK
[1] => Date: Mon, 21 Feb 2011 10:50:27 GMT
[2] => Server: Apache
[3] => Content-disposition: attachment;filename=null.csv
[4] => Content-Length: 0
[5] => Connection: close
[6] => Content-Type: text/plain
)
[wrapper_type] => http
[stream_type] => tcp_socket
[mode] => r
[unread_bytes] => 0
[seekable] =>
[uri] => http://******************************
[timed_out] =>
[blocked] => 1
[eof] =>
)
And what now? How to get the attachment programatically?
|
|
|
Re: How to read/to download attachment on an URI? [message #172515 is a reply to message #172491] |
Mon, 21 February 2011 14:12 |
Erwin Moller
Messages: 228 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 2/21/2011 11:59 AM, n00m wrote:
>
> $contents = stream_get_meta_data($handle);
>
> =================================================
>
> Array
> (
> [wrapper_data] => Array
> (
> [0] => HTTP/1.1 200 OK
> [1] => Date: Mon, 21 Feb 2011 10:50:27 GMT
> [2] => Server: Apache
> [3] => Content-disposition: attachment;filename=null.csv
> [4] => Content-Length: 0
> [5] => Connection: close
> [6] => Content-Type: text/plain
> )
>
> [wrapper_type] => http
> [stream_type] => tcp_socket
> [mode] => r
> [unread_bytes] => 0
> [seekable] =>
> [uri] => http://******************************
> [timed_out] =>
> [blocked] => 1
> [eof] =>
> )
>
>
> And what now? How to get the attachment programatically?
Hi,
Maybe something like:
$contents = stream_get_meta_data($handle);
$rawData = file_get_contents($contents["uri"]);
Regards,
Erwin Moller
--
"That which can be asserted without evidence, can be dismissed without
evidence."
-- Christopher Hitchens
|
|
|
|
Re: How to read/to download attachment on an URI? [message #172527 is a reply to message #172522] |
Mon, 21 February 2011 16:43 |
Erwin Moller
Messages: 228 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 2/21/2011 5:01 PM, n00m wrote:
>> $contents = stream_get_meta_data($handle);
>> $rawData = file_get_contents($contents["uri"]);
>
> Hi, Erwin.
> Your suggestion doesn`t seem work.
What is not working?
It should work.
What does the following produce for you:
$contents = stream_get_meta_data($handle);
$theUri = $contents["uri"];
echo "\$theUri=$theUri <br>\n";
$rawData = file_get_contents();
echo "\$rawData is:<br>\n $rawData ";
Erwin Moller
> Thanks for your reply, anyway :=)
--
"That which can be asserted without evidence, can be dismissed without
evidence."
-- Christopher Hitchens
|
|
|
Re: How to read/to download attachment on an URI? [message #172528 is a reply to message #172491] |
Mon, 21 February 2011 16:43 |
Captain Paralytic
Messages: 204 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On Feb 21, 10:59 am, n00m <n...@narod.ru> wrote:
> $contents = stream_get_meta_data($handle);
>
> =================================================
>
> Array
> (
> [wrapper_data] => Array
> (
> [0] => HTTP/1.1 200 OK
> [1] => Date: Mon, 21 Feb 2011 10:50:27 GMT
> [2] => Server: Apache
> [3] => Content-disposition: attachment;filename=null.csv
> [4] => Content-Length: 0
> [5] => Connection: close
> [6] => Content-Type: text/plain
> )
>
> [wrapper_type] => http
> [stream_type] => tcp_socket
> [mode] => r
> [unread_bytes] => 0
> [seekable] =>
> [uri] => http://******************************
> [timed_out] =>
> [blocked] => 1
> [eof] =>
> )
>
> And what now? How to get the attachment programatically?
Just use CURL
|
|
|
|
|
Re: How to read/to download attachment on an URI? [message #172557 is a reply to message #172550] |
Mon, 21 February 2011 20:35 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 2/21/2011 2:36 PM, n00m wrote:
> cURL must be OK.
> What we need is to send *** 2 *** successive requests, preserving
> session, referer etc.
> The 1st one to the page with link to URI and the 2nd -- to this URI.
Pretty easy to to with cURL - session id is in a cookie or the URL
(probably the former), and you can set the referrer to anything you want.
Of course, there's also the question as to whether you have permission
from the site owner to access the information this way; many site owners
frown on it and will block you if they find out.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|
Re: How to read/to download attachment on an URI? [message #172562 is a reply to message #172557] |
Mon, 21 February 2011 21:44 |
n00m
Messages: 25 Registered: February 2011
Karma: 0
|
Junior Member |
|
|
> Of course, there's also the question as to whether you have permission
> from the site owner to access the information this way; many site owners
> frown on it and will block you if they find out.
Remembering *** STATELESS NATURE OF HTTP *** this frowning looks very
ridiculous and silly. I.e., it's a kind of nonsense to frown on it =)
OK. Now it works. My script is:
===================================================================
$fp = fopen("cURL333.txt", "w");
$ch = curl_init();
///curl_setopt($ch, CURLOPT_URL, "http://stooq.com.ua/q/d/?s=wig20");
////curl_exec($ch);
////$info = curl_getinfo($ch);
///////curl_setopt_array($ch, $info);
curl_setopt($ch, CURLOPT_URL, "http://stooq.com.ua/q/d/l/?
s=wig20&i=d");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'GET http://stooq.com.ua/q/d/l/?s=wig20&i=d HTTP/1.0','Accept: image/
gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-
flash, application/vnd.ms-excel, application/msword, */*','Accept-
Language: ru,zh-cn;q=0.7,zh;q=0.3','Cookie: cookie_uu=110221000;
cookie_user=%3F0001dllg000011500d1300%7Cwig20','User-Agent: Mozilla/
4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)','Host:
stooq.com.ua','Proxy-Connection: Keep-Alive'));
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec($ch);
curl_close($ch);
fclose($fp);
================================================================
But I'm a total ***newb*** in PHP and got those headers by some other
way.
How can we get them ***automatically*** after hitting the 1st page???
===============================================
P.S. The saved file looks like this:
===============================================
HTTP/1.1 200 OK
Date: Mon, 21 Feb 2011 21:24:38 GMT
Server: Apache
Content-disposition: attachment;filename=wig20_d.csv
Transfer-Encoding: chunked
Content-Type: text/plain
Date,Open,High,Low,Close,Volume
1991-04-16,100,100,100,100,325
1991-04-23,95.7,95.7,95.7,95.7,5905
1991-04-30,93.5,93.5,93.5,93.5,7162
1991-05-14,92.9,92.9,92.9,92.9,18300
1991-05-21,95.5,95.5,95.5,95.5,14750
1991-05-28,94.6,94.6,94.6,94.6,31440
1991-06-04,95.8,95.8,95.8,95.8,12396
1991-06-11,95,95,95,95,26247
........
........
........
|
|
|
Re: How to read/to download attachment on an URI? [message #172565 is a reply to message #172562] |
Mon, 21 February 2011 22:05 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 2/21/2011 4:44 PM, n00m wrote:
>> Of course, there's also the question as to whether you have permission
>> from the site owner to access the information this way; many site owners
>> frown on it and will block you if they find out.
>
> Remembering *** STATELESS NATURE OF HTTP *** this frowning looks very
> ridiculous and silly. I.e., it's a kind of nonsense to frown on it =)
>
Not at all - it is commonly done, and pretty easy - I've done it for
clients who have had their content reused without their permission. It
is also illegal in most (all?) countries.
>
> OK. Now it works. My script is:
> ===================================================================
> $fp = fopen("cURL333.txt", "w");
>
> $ch = curl_init();
>
> ///curl_setopt($ch, CURLOPT_URL, "http://stooq.com.ua/q/d/?s=wig20");
> ////curl_exec($ch);
> ////$info = curl_getinfo($ch);
> ///////curl_setopt_array($ch, $info);
>
> curl_setopt($ch, CURLOPT_URL, "http://stooq.com.ua/q/d/l/?
> s=wig20&i=d");
> curl_setopt($ch, CURLOPT_HEADER, true);
>
>
>
> curl_setopt($ch, CURLOPT_HTTPHEADER, array(
> 'GET http://stooq.com.ua/q/d/l/?s=wig20&i=d HTTP/1.0','Accept: image/
> gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-
> flash, application/vnd.ms-excel, application/msword, */*','Accept-
> Language: ru,zh-cn;q=0.7,zh;q=0.3','Cookie: cookie_uu=110221000;
> cookie_user=%3F0001dllg000011500d1300%7Cwig20','User-Agent: Mozilla/
> 4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)','Host:
> stooq.com.ua','Proxy-Connection: Keep-Alive'));
>
>
>
> curl_setopt($ch, CURLOPT_FILE, $fp);
>
> curl_exec($ch);
> curl_close($ch);
> fclose($fp);
> ================================================================
>
> But I'm a total ***newb*** in PHP and got those headers by some other
> way.
> How can we get them ***automatically*** after hitting the 1st page???
>
> ===============================================
> P.S. The saved file looks like this:
> ===============================================
>
> HTTP/1.1 200 OK
> Date: Mon, 21 Feb 2011 21:24:38 GMT
> Server: Apache
> Content-disposition: attachment;filename=wig20_d.csv
> Transfer-Encoding: chunked
> Content-Type: text/plain
>
> Date,Open,High,Low,Close,Volume
> 1991-04-16,100,100,100,100,325
> 1991-04-23,95.7,95.7,95.7,95.7,5905
> 1991-04-30,93.5,93.5,93.5,93.5,7162
> 1991-05-14,92.9,92.9,92.9,92.9,18300
> 1991-05-21,95.5,95.5,95.5,95.5,14750
> 1991-05-28,94.6,94.6,94.6,94.6,31440
> 1991-06-04,95.8,95.8,95.8,95.8,12396
> 1991-06-11,95,95,95,95,26247
> .......
> .......
> .......
>
>
>
>
As for retrieving the information, if you've got the header info for the
file, you should be able to fetch the file itself with cURL.
But as it's pretty obvious from your answer above that you don't have
permission to do this, sorry, I won't help you.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|
Re: How to read/to download attachment on an URI? [message #172585 is a reply to message #172565] |
Tue, 22 February 2011 01:33 |
n00m
Messages: 25 Registered: February 2011
Karma: 0
|
Junior Member |
|
|
Done.
===========================================
<?php
$url_1 = "http://stooq.com.ua/q/d/?s=wig20";
$url_2 = "http://stooq.com.ua/q/d/l/?s=wig20&i=d";
$headers = array(
'GET ' . $url_1 . ' HTTP/1.0',
'Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
application/x-shockwave-flash, application/vnd.ms-excel, application/
msword, */*',
'Accept-Language: ru,zh-cn;q=0.7,zh;q=0.3',
'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;
SV1)',
'Proxy-Connection: Keep-Alive'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_1);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, $url_2);
$headers[0] = 'GET ' . $url_2 . ' HTTP/1.0';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
$fp = fopen("ccc.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec($ch);
curl_close($ch);
fclose($fp);
echo "\n\n\nDone!\n";
fgetc(STDIN);
?>
|
|
|
|
Re: How to read/to download attachment on an URI? [message #172616 is a reply to message #172586] |
Tue, 22 February 2011 08:01 |
Erwin Moller
Messages: 228 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 2/22/2011 2:38 AM, n00m wrote:
> ==================================================
> Btw in VBA it can be done even (much) easier:
> ==================================================
>
> Sub w2()
> Dim o As New WinHttp.WinHttpRequest
> o.Open "GET", "http://stooq.com.ua/q/d/?s=wig20", False
> o.Send
> o.Open "GET", "http://stooq.com.ua/q/d/l/?s=wig20&i=d", False
> o.Send
> MsgBox o.ResponseText
> Set o = Nothing
> End Sub
>
> ==================================================
Hi,
Glad you figured it out.
SO it was the Session that was demanded to proceed? That is quite common
indeed.
About VBA being "much easier": It seem to me VBA is sending cookies as a
default setting when using WinHttp.WinHttpRequest.
I am not sure I would classify that behavior as "much easier". It is
just "much easier" in this case.
Personally I like the detailed control one has when using CURL.
Regards,
Erwin Moller
--
"That which can be asserted without evidence, can be dismissed without
evidence."
-- Christopher Hitchens
|
|
|
Re: How to read/to download attachment on an URI? [message #172628 is a reply to message #172616] |
Tue, 22 February 2011 15:23 |
n00m
Messages: 25 Registered: February 2011
Karma: 0
|
Junior Member |
|
|
Erwin, of course, you are right.
PHP's fine granularity of control over everything is much more
powerful thing.
It's a senseless idea to compare it against VBA's web/http candies.
> SO it was the Session that was demanded to proceed?
Yes. Precisely in this case it's only cookies and not e.g. REFERER
header.
|
|
|