PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 15th, 2007, 10:27 PM
tekomp tekomp is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 52 tekomp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 h 41 m 55 sec
Reputation Power: 5
Trouble with a cURL request in PHP

Hi,

I'm trying to do a cURL request with curl_init() and having problems with certain URLs that I can get to work fine through the shell. The request is for a URL with a query string that should do a 302 redirect, but when cURL tries to make the request, it first does a request for only the hostname. The problem is that the page on the base hostname, returns a 403.

Here's what I have:

PHP Code:
 $url 'http://example.com/cgi-bin/script.cgi?url=http://www.example2.com';

$ch curl_init();
curl_setopt($chCURLOPT_URL$url);
curl_setopt($chCURLOPT_VERBOSEtrue);
curl_setopt($chCURLOPT_HEADERtrue);
curl_setopt($chCURLOPT_NOBODYtrue);
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
curl_setopt($chCURLOPT_TIMEOUT20);
curl_setopt($chCURLOPT_AUTOREFERERtrue);
curl_setopt($chCURLOPT_FOLLOWLOCATIONtrue);
$ret curl_exec($ch);
$info curl_getinfo($ch);
curl_close($ch);

print_r($info); 


Here's the output:

Code:
* About to connect() to example.com port 80 (#0)
*   Trying 12.12.12.12... * connected
* Connected to example.com (12.12.12.12) port 80 (#0)
> HEAD /cgi-bin/script.cgi?url=http://example2.com HTTP/1.1
Host: example.com
Accept: */*

< HTTP/1.1 403 Forbidden
< Date: Wed, 16 May 2007 01:53:02 GMT
< Server: Apache
< Connection: close
< Content-Type: text/html; charset=iso-8859-1
* Closing connection #0

Array
(
    [url] => http://example.com/cgi-bin/script.cgi?url=http://www.example2.com
    [content_type] => text/html; charset=iso-8859-1
    [http_code] => 403
    [redirect_count] => 0
)



If I do it through the shell, it shows the redirect just fine:

Code:
%curl -i 'http://example.com/cgi-bin/script.cgi?url=http://www.example2.com'
HTTP/1.1 302 Found
Date: Wed, 16 May 2007 02:17:22 GMT
Server: Apache
Location: http://www.example2.com
Content-Length: 232
Connection: close
Content-Type: text/html; charset=iso-8859-1


Any ideas on how I can make it so it doesn't do that request on the hostname and uses the entire URL, in order to not get that 403?

Thanks.

Reply With Quote
  #2  
Old May 16th, 2007, 02:30 AM
tekomp tekomp is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 52 tekomp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 h 41 m 55 sec
Reputation Power: 5
I was able to get this working using:

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');

It was my understanding that GET was the default request method, so not sure why this changes it, but at any rate, it works now.

Reply With Quote
  #3  
Old May 16th, 2007, 07:34 AM
printf printf is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2005
Posts: 1,586 printf User rank is Captain (20000 - 30000 Reputation Level)printf User rank is Captain (20000 - 30000 Reputation Level)printf User rank is Captain (20000 - 30000 Reputation Level)printf User rank is Captain (20000 - 30000 Reputation Level)printf User rank is Captain (20000 - 30000 Reputation Level)printf User rank is Captain (20000 - 30000 Reputation Level)printf User rank is Captain (20000 - 30000 Reputation Level)printf User rank is Captain (20000 - 30000 Reputation Level)printf User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 4 Weeks 1 h 32 m 14 sec
Reputation Power: 270
Just so you know, next time...

Anytime you set the option...

PHP Code:
 curl_setopt($chCURLOPT_NOBODYtrue); 


CURLOPT_NOBODY automatically shifts the request to a HEAD, type request, so most servers don't allow HEAD type requests, results most times in 403 errors, so you use...

PHP Code:
 curl_setopt($chCURLOPT_CUSTOMREQUEST'GET'); 


So cURL, will send the request as GET or POST, but still only really make a HEAD request because the CURLOPT_NOBODY flag was set to true, which causes cURL to read only up to the end of the response header.

Reply With Quote
  #4  
Old February 14th, 2008, 05:46 PM
davidyin davidyin is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Posts: 1 davidyin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 32 sec
Reputation Power: 0
Quote:
Originally Posted by printf
Just so you know, next time...

Anytime you set the option...

PHP Code:
 curl_setopt($chCURLOPT_NOBODYtrue); 


CURLOPT_NOBODY automatically shifts the request to a HEAD, type request, so most servers don't allow HEAD type requests, results most times in 403 errors, so you use...

PHP Code:
 curl_setopt($chCURLOPT_CUSTOMREQUEST'GET'); 


So cURL, will send the request as GET or POST, but still only really make a HEAD request because the CURLOPT_NOBODY flag was set to true, which causes cURL to read only up to the end of the response header.


It really works on my PHP tools.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Trouble with a cURL request in PHP


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
Stay green...Green IT