Jump to content

using libcurl in my php script help....


test006

Recommended Posts

Hi all,
I am currently using curl(libcurl) in my php to login to a remote website and download a file.
Now i have the first part of the script working i.e. i can login to website using my php script but i cannot download the file. I need to use curl since i need to submit username and password as http post.
The problem is that when you click on the link to download the file, the server sends the http header which says:
[code]
HTTP/1.x 200 OK
Date: Fri, 12 May 2006 14:04:09 GMT
Server: Apache/2.0.54 (Fedora)
X-Powered-By: PHP/4.4.0
Expires: 0
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
content-disposition: attachment; filename=testing.xls
Connection: close
Transfer-Encoding: chunked
Content-Type: application/x-octet-stream
[/code]
How do i deal with content-disposition header with curl? Can anyone help me?
When is normal browser you can a dialog box which promts you to save the file on your harddrive...
Thanks in advance....

Link to comment
Share on other sites

Don't include the header and write out to a file. Something like this code:

[a href=\"http://www.weberdev.com/get_example-4009.html\" target=\"_blank\"]http://www.weberdev.com/get_example-4009.html[/a]

That code is not perfect but should work. Remember to close the opened file (which the code doesn't do). Also that function doesn't return anything and $local_path is not used.
Link to comment
Share on other sites

[!--quoteo(post=373517:date=May 13 2006, 09:37 AM:name=toplay)--][div class=\'quotetop\']QUOTE(toplay @ May 13 2006, 09:37 AM) [snapback]373517[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Don't include the header and write out to a file. Something like this code:

[a href=\"http://www.weberdev.com/get_example-4009.html\" target=\"_blank\"]http://www.weberdev.com/get_example-4009.html[/a]

That code is not perfect but should work. Remember to close the opened file (which the code doesn't do). Also that function doesn't return anything and $local_path is not used.
[/quote]
Hi,
I have tried this, and header is not the problem.
When the webserver sends http header with content-disposition i need curl to able to handle this and download the file.
The normal browser basically detects this and pops a save the file dialog box.

How can i get this with curl.

Thanks in advance
Link to comment
Share on other sites

Look at the code from the link I've already provided. Did you really open and specify a file for cURL to download the file into?

Otherwise, post your EXACT code you're using if you want members on this forum to help you easier and quicker.
Link to comment
Share on other sites

This forum has been doing that sometimes. I tried to post code on another post and got the same thing. But when I took off phpinfo function it allowed me to post. So, it seems some things aren't allowed to be posted or it just gets confused.

You can't PM me because it will do the same thing.

Try posting again or just put the code on your serve and provide a link to the source code.

Link to comment
Share on other sites

[!--quoteo(post=375494:date=May 20 2006, 08:35 AM:name=toplay)--][div class=\'quotetop\']QUOTE(toplay @ May 20 2006, 08:35 AM) [snapback]375494[/snapback][/div][div class=\'quotemain\'][!--quotec--]
This forum has been doing that sometimes. I tried to post code on another post and got the same thing. But when I took off phpinfo function it allowed me to post. So, it seems some things aren't allowed to be posted or it just gets confused.

You can't PM me because it will do the same thing.

Try posting again or just put the code on your serve and provide a link to the source code.
[/quote]
Hi,
When you say phpinfo you mean phpinfo() as part of my code. I do have this part of my code that i am trying to post.
I do not have a server thats on the internet that i can use to post my code. All my code is on my local PC which in a private network and sits behind a dynamic public IP address on my router.
Can i email you the code?
Sorry for the trouble.

Thanks
Link to comment
Share on other sites

[!--quoteo(post=375601:date=May 20 2006, 07:28 PM:name=toplay)--][div class=\'quotetop\']QUOTE(toplay @ May 20 2006, 07:28 PM) [snapback]375601[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Just take out the phpinfo function and you should be able to post it here.
[/quote]
I do not have phpinfo function in my code.
If you mean this:
echo "phpinfo()";

I do not have this part of my code.....

I have tried different things i.e removing different parts of the code but it still gives me error saying i do not have permission to post on this server.

Its a pain.

Thanks
Link to comment
Share on other sites

[!--quoteo(post=375596:date=May 20 2006, 05:10 PM:name=test006)--][div class=\'quotetop\']QUOTE(test006 @ May 20 2006, 05:10 PM) [snapback]375596[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Hi,
When you say phpinfo you mean phpinfo() as part of my code. I do have this part of my code that i am trying to post.
...
Thanks
[/quote]
Your earlier post said that did use it and that's why I responded saying to take it out.

It's not my site. Sorry, that you're having problems.

I can't remember the sites but there are some that let you post code temporarily and give you a link to the code for posting into forums and such. If you find one of those, then do that.
Link to comment
Share on other sites

[!--quoteo(post=375610:date=May 20 2006, 07:53 PM:name=toplay)--][div class=\'quotetop\']QUOTE(toplay @ May 20 2006, 07:53 PM) [snapback]375610[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Your earlier post said that did use it and that's why I responded saying to take it out.

It's not my site. Sorry, that you're having problems.

I can't remember the sites but there are some that let you post code temporarily and give you a link to the code for posting into forums and such. If you find one of those, then do that.
[/quote]
Yes i understand its not your site.
O.K lets do it another way.
I will describe my code and what i am doing.

My first code block uses curl to login to the remote website. This works fine i.e. i can login using the curl.

Now once i ma logged in i use the URL to get the file from the remote website.
URL is something like this:
[a href=\"http://www.somewebsite.com/?page=shop/pricelist&print=1&\" target=\"_blank\"]http://www.somewebsite.com/?page=shop/pricelist&print=1&[/a]
I use/try curl to download the file from the above URL.
so my curl code looks like this
I set CURLOPT_HEADER to 0. (I have tried 1 as well)
And than CURLOPT_URL to url shown above.
Than i do $data = curl_exec($ch)
So in theory the file i am trying to get should be stored in variable $data.
I to close_curl

Now i use fopen to open file and write $data to my file and than close the file.

The thing is i do not get anything in my file.
So my orginal question was how do i deal with http header sent by server that contains content-disposition:?

Beacuse i have used curl before to download files before but the server are not using content-disposition: header.

thanks...hope this helps....

i will try to find some hosting website and than post my exact code....




Link to comment
Share on other sites

Just try it like the code posted in the link provided in my first post here. Open a file first (for output) and use CURLOPT_FILE with the file handle of the opened file.

1) open file for writing.
2) initialize curl.
3) set CURLOPT_FILE with the handle of the file opened for output.
4) Turn CURLOPT_HEADER off (0).
5) Specify the URL using CURLOPT_URL.
6) Execute curl.
7) close curl
8) close file you opened for output.
9) Check the file contents.
Link to comment
Share on other sites

[!--quoteo(post=375644:date=May 20 2006, 10:31 PM:name=toplay)--][div class=\'quotetop\']QUOTE(toplay @ May 20 2006, 10:31 PM) [snapback]375644[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Just try it like the code posted in the link provided in my first post here. Open a file first (for output) and use CURLOPT_FILE with the file handle of the opened file.

1) open file for writing.
2) initialize curl.
3) set CURLOPT_FILE with the handle of the file opened for output.
4) Turn CURLOPT_HEADER off (0).
5) Specify the URL using CURLOPT_URL.
6) Execute curl.
7) close curl
8) close file you opened for output.
9) Check the file contents.
[/quote]
O.K i will give it a try and get back to you...

Thanks
Link to comment
Share on other sites

Good, ok. You tried it the other way and it's not working for you, so you must try a different method. You can't do the same thing and expect it to work. Maybe your code didn't work because you don't show that you used the CURLOPT_RETURNTRANSFER to actually return the data to your $data variable (when cURL is executed).

The posting issue has been addressed in this topic:
[a href=\"http://www.phpfreaks.com/forums/index.php?showtopic=90469\" target=\"_blank\"]http://www.phpfreaks.com/forums/index.php?showtopic=90469[/a]

The site where you can post code temporarily is called: [a href=\"http://pastebin.com\" target=\"_blank\"]http://pastebin.com[/a]

Code example: [a href=\"http://pastebin.com/729709\" target=\"_blank\"]http://pastebin.com/729709[/a]

Also, if the sites you're accessing is using cookies, you have to account for that too. See this topic:
[a href=\"http://www.phpfreaks.com/forums/index.php?showtopic=93572\" target=\"_blank\"]http://www.phpfreaks.com/forums/index.php?showtopic=93572[/a]

Link to comment
Share on other sites

[!--quoteo(post=375769:date=May 21 2006, 10:03 AM:name=toplay)--][div class=\'quotetop\']QUOTE(toplay @ May 21 2006, 10:03 AM) [snapback]375769[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Good, ok. You tried it the other way and it's not working for you, so you must try a different method. You can't do the same thing and expect it to work. Maybe your code didn't work because you don't show that you used the CURLOPT_RETURNTRANSFER to actually return the data to your $data variable (when cURL is executed).

The posting issue has been addressed in this topic:
[a href=\"http://www.phpfreaks.com/forums/index.php?showtopic=90469\" target=\"_blank\"]http://www.phpfreaks.com/forums/index.php?showtopic=90469[/a]

The site where you can post code temporarily is called: [a href=\"http://pastebin.com\" target=\"_blank\"]http://pastebin.com[/a]

Code example: [a href=\"http://pastebin.com/729709\" target=\"_blank\"]http://pastebin.com/729709[/a]

Also, if the sites you're accessing is using cookies, you have to account for that too. See this topic:
[a href=\"http://www.phpfreaks.com/forums/index.php?showtopic=93572\" target=\"_blank\"]http://www.phpfreaks.com/forums/index.php?showtopic=93572[/a]
[/quote]
Hi,
First of all thanks for all the info.
I had another look through my code and it seems that my login code actually logs me into the website but it does not set the cookie in my cookie file.

Here is my login script:
[a href=\"http://pastebin.com/732774\" target=\"_blank\"]http://pastebin.com/732774[/a]

Once i execute the above code i can see that i am logged into the website.
When i look at /tmp/cs file i should see the cookie there but it empty. i have given it all the permission i have even done chmod 777....
Now examing the http header that i get back from server is:
Set-Cookie: ps_sid=5e1d4348d7e193396c24de759f851158

But i cannot see this in my cs file.
In order for my to download the file i need this cookie to be set in the cs file...

Thanks
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.