473,883 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CURL and PHP Redirect

Hi All,

I have a small curl function which spits out a bunch of headers.

The CURL Code is:

$url="http://blahblah.com?Lo gon";
$ch = curl_init();
curl_setopt($ch , CURLOPT_URL, $url);
curl_setopt($ch , CURLOPT_FOLLOWL OCATION, 1);
curl_setopt($ch , CURLOPT_RETURNT RANSFER, 1);
curl_setopt($ch , CURLOPT_USERAGE NT, $_SERVER["HTTP_USER_AGEN T"]);
curl_setopt($ch , CURLOPT_REFERER ,
"http://blahblah.com/default.asp?v=" );
curl_setopt($ch , CURLOPT_VERBOSE , 0);
curl_setopt($ch , CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIE LDS,
"FormId=0&Confi g=0&RedirectURL =$RedirectURL&I dentification=$ UserID&Verifica tion=$Password& DB=$DB&Remember Logon=1&UseEnha ncedContent=1") ;
curl_setopt ($ch, CURLOPT_COOKIEJ AR, 'Ocookie.txt');
$content = curl_exec ($ch); # This returns HTML
curl_close ($ch);
print $content;
The HTML sspit out by the above code is:

HTTP/1.1 100 Continue Server: Microsoft-IIS/5.1 Date: Fri, 17 Mar 2006
00:14:25 GMT X-Powered-By: ASP.NET HTTP/1.1 200 OK Server:
Microsoft-IIS/5.1 Date: Fri, 17 Mar 2006 00:14:25 GMT X-Powered-By:
ASP.NET Connection: close Content-Type: text/html Refresh: 0;
URL=http://blahblah.com/default.asp?v= Set-Cookie: osaa=guest;
expires=Monday, 16-Mar-2009 00:14:25 GMT; path=/ Set-Cookie:
osab=guest; expires=Monday, 16-Mar-2009 00:14:25 GMT; path=/

All this is printed along with the existing HTML on which the above
curl code is written. How do I redirect the existing page to the one
setup in the headers by making sure of passing the whole header
information which is spit out by the curl code?

Thanks

Mar 17 '06 #1
12 26524

ni*******@gmail .com wrote:
Hi All,

I have a small curl function which spits out a bunch of headers.

The CURL Code is:

$url="http://blahblah.com?Lo gon";
$ch = curl_init();
curl_setopt($ch , CURLOPT_URL, $url);
curl_setopt($ch , CURLOPT_FOLLOWL OCATION, 1);
curl_setopt($ch , CURLOPT_RETURNT RANSFER, 1);
curl_setopt($ch , CURLOPT_USERAGE NT, $_SERVER["HTTP_USER_AGEN T"]);
curl_setopt($ch , CURLOPT_REFERER ,
"http://blahblah.com/default.asp?v=" );
curl_setopt($ch , CURLOPT_VERBOSE , 0);
curl_setopt($ch , CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIE LDS,
"FormId=0&Confi g=0&RedirectURL =$RedirectURL&I dentification=$ UserID&Verifica tion=$Password& DB=$DB&Remember Logon=1&UseEnha ncedContent=1") ;
curl_setopt ($ch, CURLOPT_COOKIEJ AR, 'Ocookie.txt');
$content = curl_exec ($ch); # This returns HTML
curl_close ($ch);
print $content;
The HTML sspit out by the above code is:

HTTP/1.1 100 Continue Server: Microsoft-IIS/5.1 Date: Fri, 17 Mar 2006
00:14:25 GMT X-Powered-By: ASP.NET HTTP/1.1 200 OK Server:
Microsoft-IIS/5.1 Date: Fri, 17 Mar 2006 00:14:25 GMT X-Powered-By:
ASP.NET Connection: close Content-Type: text/html Refresh: 0;
URL=http://blahblah.com/default.asp?v= Set-Cookie: osaa=guest;
expires=Monday, 16-Mar-2009 00:14:25 GMT; path=/ Set-Cookie:
osab=guest; expires=Monday, 16-Mar-2009 00:14:25 GMT; path=/

All this is printed along with the existing HTML on which the above
curl code is written.
IIRC, this is happening as you have used curl_setopt($ch ,
CURLOPT_HEADER, 1);. Set it to 0.
How do I redirect the existing page to the one
setup in the headers by making sure of passing the whole header
information which is spit out by the curl code?


CURLOPT_FOLLOWL OCATION should handle that case, IMHO. But, also you
have wide range of options to store and set HTTP headers in libcurl.
If I'm right, you're missing CURLOPT_COOKIEF ILE (should be set to the
same file of cookie jar) as it is needed for reading/setting the cookie
headers when sending requests. Make sure the cookie file is readable
and writable.

FWIW, for testing purpose, you may enable verbose log:

$fp_err = fopen('verbose_ file.txt', 'ab+');
fwrite($fp_err, date('Y-m-d H:i:s')."\n\n") ; //add timestamp to the
verbose log
curl_setopt($ch , CURLOPT_VERBOSE , 1);
curl_setopt($ch , CURLOPT_FAILONE RROR, true);
curl_setopt($ch , CURLOPT_STDERR, $fp_err);

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Mar 17 '06 #2
Hi,

CURLOPT_FOLLOWL OCATION doesnt seem to handle the case of redirection.
Any other ideas? Verbose log was useful to know the header information.

Thanks

Mar 17 '06 #3
Also, CURLOPT_COOKIEF ILE works great but seems to make the page load
slower.. Is there a faster alternative to it? :)

Mar 17 '06 #4
ni*******@gmail .com wrote:
Also, CURLOPT_COOKIEF ILE works great but seems to make the page load
slower.. Is there a faster alternative to it? :)


Among the above two questions which is your concern now? I guess,
both of them are now sorted out.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Mar 21 '06 #5
Hi,

I was able to use CURLOPT_COOKIEF ILE to write cookies and do my
redirections fine. But my only concern now is it takes a lot of time
for CURLOPT_COOKIEF ILE to update cookies. I just wanted to know how we
can speed up this process?

Thanks
R. Rajesh Jeba Anbiah wrote:
ni*******@gmail .com wrote:
Also, CURLOPT_COOKIEF ILE works great but seems to make the page load
slower.. Is there a faster alternative to it? :)


Among the above two questions which is your concern now? I guess,
both of them are now sorted out.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/


Mar 21 '06 #6
ni*******@gmail .com wrote:
Hi,

I was able to use CURLOPT_COOKIEF ILE to write cookies and do my
redirections fine. But my only concern now is it takes a lot of time
for CURLOPT_COOKIEF ILE to update cookies. I just wanted to know how we
can speed up this process?

<snip>

I guess, GG ate my yesterday reply. I mentioned that you should
check verbose log and also check if there is any locking issues with
the file. Also, try to place the cookie file in the current path.

Also, CURLOPT_COOKIEF ILE is for setting http headers; for setting
http headers, it will fetch the cookie from that file.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Mar 22 '06 #7
Hi,

Following is the verbose log for my curl transfer with progress meter
on. I am unable to figure out why IIS is taking 4 seconds to spit out
the response..any ideas?

2006-03-22 15:09:00

* About to connect() to www2.lib.ncsu.e du port 80
* Trying 152.1.79.69... * connected
* Connected to www2.lib.ncsu.e du (152.1.79.69) port 80
GET /catalog/?view=&Nty=1&N= 0&Ntk=Keyword&N tt=cats&No= HTTP/1.1 Host: www2.lib.ncsu.e du
Accept: */*
Cookie: JSESSIONID=39F9 A1F5288148C802B D3B92C2978CFF

< HTTP/1.1 200 OK
< Date: Wed, 22 Mar 2006 21:10:48 GMT
< Server: Apache/2.0.52 (Unix) JRun/4.0 mod_jk/1.2.8
< Transfer-Encoding: chunked
< Content-Type: text/html;charset=UT F-8
% Total % Received % Xferd Average Speed Time Time Time
Current
Dload Upload Total Spent Left
Speed
100 1187 0 1187 0 0 5623 0 --:--:-- --:--:--
--:--:-- 5623
100 25210 0 25210 0 0 89473 0 --:--:-- --:--:--
--:--:-- 335k* Connection #0 to host www2.lib.ncsu.e du left intact

* Closing connection #0
2006-03-22 15:09:00

* About to connect() to 172.19.10.249 port 80
* Trying 172.19.10.249.. . * connected
* Connected to 172.19.10.249 (172.19.10.249) port 80 GET /abc/xyztest.asp?v= HTTP/1.1

Host: 172.19.10.249
Accept: */*
Cookie: ASPSESSIONIDASS TQTRR=MNOLKGNAN DGKFMMNCAGGNPHD ;
osah=ALBANY+PAR K; osag=Chicago+Pu blic+Library; osaf=1; osae=1;
osad=2644; osac=doe; osab=john; osaa=JOHN

% Total % Received % Xferd Average Speed Time Time Time
Current
Dload Upload Total Spent Left
Speed
0 0 0 0 0 0 0 0 --:--:-- 0:00:01
--:--:-- 0
0 0 0 0 0 0 0 0 --:--:-- 0:00:02
--:--:-- 0
0 0 0 0 0 0 0 0 --:--:-- 0:00:03
--:--:-- 0
0 0 0 0 0 0 0 0 --:--:-- 0:00:04
--:--:-- 0< HTTP/1.1 200 OK
< Server: Microsoft-IIS/5.1
< Date: Wed, 22 Mar 2006 21:08:57 GMT
< X-Powered-By: ASP.NET
< Content-Length: 29727
< Content-Type: text/html
< Cache-control: private

100 29727 100 29727 0 0 7270 0 0:00:04 0:00:04
--:--:-- 9670* Connection #0 to host 172.19.10.249 left intact

* Closing connection #0
Thanks

Mar 22 '06 #8
ni*******@gmail .com wrote:
Hi,

Following is the verbose log for my curl transfer with progress meter
on. I am unable to figure out why IIS is taking 4 seconds to spit out
the response..any ideas?

<snip>

I haven't tried turning on progress meter option yet and so I'm not
sure about it. Not sure, it affects the speed.

Could you try manually with browser and cross check the http headers
with the one set via curl; and tweak the options so that it sends
exactly the same http headers sent by browser. And, what's the time
taken for browser?

Could you post the result of print_r(curl_ge tinfo($ch)); I guess,
it's taking too many redirections.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Mar 23 '06 #9
Hi,

The result of print_r(curl_ge tinfo($ch)); is as follows:

Array ( [url] => http://localhost/abc/default.asp?xyz = [http_code] => 0
[header_size] => 0 [request_size] => 0 [filetime] => 0
[ssl_verify_resu lt] => 0 [redirect_count] => 0 [total_time] => 0
[namelookup_time] => 0 [connect_time] => 0 [pretransfer_tim e] => 0
[size_upload] => 0 [size_download] => 0 [speed_download] => 0
[speed_upload] => 0 [download_conten t_length] => 0
[upload_content_ length] => 0 [starttransfer_t ime] => 0 [redirect_time]
=> 0 )

I will cross check the http headers and try to tweak curl options..

Thanks

Mar 24 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3361
by: D Benfer | last post by:
Hi all, Im a bit thrown by curl. I want to submit some order data on an ecommerce site via a form to a php page, where the data is saved to a database. Then I want to use curl to submit the form data I just saved (and redirect the user) to my secure credit card gateway. Problem is, they have their own secure payment screen where the credit card information is entered. I can't just retrieve the variable with a transaction...
9
8775
by: Dave Martin | last post by:
I've successfully used CURL to maneuver around and through sites but a new site I've been trying to work with has got me stumped. To retrieve the data I'm trying to get a login is required. If you go to any page in the "member" area without logging in you get directed to a https connection and hit login.php (ie: you end up at https://www.domain.com/login.php). The problem, I believe, lies in the fact that the form on that page...
5
19419
by: Dave | last post by:
Hi All, Can anyone suggest a reliable solution to php redirect?: header("Location: $redir_url"); if the above times out in $timeout seconds header("Location: $alt_redir_url"); Many thanks,
0
5813
by: susb8383 | last post by:
Hi, Is there a way to use cURL to redirect, but have the URL change to the new page? I have the following, but the URL remains as that of the original page after the redirect: <?php $url = "http://www.mydomain.com/test3.php"; $ch = curl_init(); // initialize curl handle
0
1523
by: existatus | last post by:
In a form there are entries like this: <input type="text" name="first_name" size="24" /> <input type="text" name="last_name" size="24" /> <input type="hidden" name="must_have" value="12345" /> <input type="hidden" name="recipient" value="myemail@comcast.net" /> <input type="hidden" name="redirect" value="http://mywebsite.com" /> <input type="hidden" name="subject" value="Topic In Question" />
56
7269
by: UKuser | last post by:
Hi, I'm not sure if this can be done as I've searched the web and this forum. I am using an online merchant provider and I must post certain variables to their webforms through a form on my website. The issue is that I need to gauge whether a user has any items in their basket to decide which page I redirect them too. I could
5
2987
by: ania.malicka85 | last post by:
Hello, I am playing with automatic redirection with PHP. As with GET is fairly simple (<meta http-equiv="refresh" content="3;url="http:// www.google.com/search?q=php"> how can I do similar thing with POST (lets assume that I want to add q = php to post variables). How can I do this? regards, Ann
3
15682
by: Sarah | last post by:
I was wondering if someone might be able to help me with this issue. I have a feeling this has something to do with my host's server settings as I used to be able to get CURL to follow redirects by setting CURLOPT_FOLLOWLOCATION set to true. I had a problem with my host's updating something in the past that gave me the error "CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set". My host worked on the...
3
6306
by: buzz2050 | last post by:
Hi all, I am using cURL for the first time. I need to login to a site and my cURL code to do the same is as follows: //curlScript.php <?php function getContent($url, $referer, $cookie_file_name, $post_fields='') {
0
9944
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9796
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10757
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10860
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10420
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9583
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6002
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4620
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4225
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.