PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPHP Development
The ASP Free website provides in-depth information on the latest developer tools available from Microsoft. Our cadre of writers, highly experienced industry experts, reveals the best ways to use established technologies as well as new and emerging technologies. Our coverage of Microsoft's development and administration technologies is among the most respected in the IT industry today.

ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month!
Download and Activate to enter!

Intel® Graphics Performance Analyzers is a powerful tool suite for analyzing and optimizing your games, media, and graphics-intensive applications. Used by some of the best developers on the planet, Intel GPA lets you maximize your app’s performance.


Tutorials
| Forums

Download to Enter
| Contest Rules

DOWNLOAD INTEL® GPA FOR FREE

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: 7
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: 7
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 34 m 20 sec
Reputation Power: 273
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

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.

© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap