我的PHP CURL请求有什么问题?

时间:2012-03-01 11:52:39

标签: php curl

我的团队使用basecamp进行项目管理,我是管理员。我试图找出如何使用basecamp REST api,以便我可以创建一些我可以在内部网页上使用的自定义插件,以便团队成员可以直接向basecamp写评论。

我是CURL和PHP的新手,所以我开始非常简单。只是尝试首先尝试取回项目列表。我已经打了一堵砖墙。

以下是API文档:http://developer.37signals.com/basecamp/

如果我在网络浏览器中访问https:// [mycompanyname] .basecamphq.com / projects.xml,弹出窗口会请求我的用户名和密码。而不是我使用的u / pw,我输入了我的api令牌和密码" X"就像他们指示的那样。它工作正常,我得到了所有项目的XML。大。所以我知道api令牌很好。

现在尝试使用PHP和CURL执行此操作。继承我的代码

$process = curl_init();
$username = '[my api token]';
$password = 'X';

curl_setopt($process, CURLOPT_URL, 'https://[mycompanyname].basecamphq.com/projects.xml'); 
curl_setopt($process, CURLOPT_HEADER, 1);                                                                           
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/xml', 'Content-Type: application/xml', 'Authorization: Basic [my api token]=='));        
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);                                                
curl_setopt($process, CURLOPT_TIMEOUT, 30);                                                                         
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);                                                                
curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);

$response = curl_exec($process);  
curl_close($process);

echo '<pre>';
print_r($response);

以下是我得到的回复:

HTTP/1.1 401 Unauthorized
Server: nginx/1.0.8
Date: Thu, 01 Mar 2012 11:20:12 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Status: 401 Unauthorized
Strict-Transport-Security: max-age=31536000
WWW-Authenticate: Basic realm=[removed]
Cache-Control: no-cache
X-UA-Compatible: IE=Edge,chrome=1
X-Runtime: 0.014529
X-Throttle-Count: 1
X-Throttle-Max: 500
X-Throttle-Horizon: 2012-03-01T11:20:20Z

HTTP Basic: Access denied.

我无法弄清楚原因。当我检查标题/响应并比较两者时,它们看起来没什么相似之处。

来自PHP尝试的标头和响应

Request URL:http://localhost/sandbox/basecamp.php
Request Method:GET
Status Code:200 OK
Request Headersview parsed
GET /sandbox/basecamp.php HTTP/1.1
Host: localhost
Connection: keep-alive
Cache-Control: max-age=0
User-Agent: Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.46 Safari/535.11
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,es;q=0.6
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: MoodleSessionTest=M0QYDbdGlc; MoodleSession=4r2c9g6eset4tg1hhk6h3b8tr2; MOODLEID_=%25EE%25C3%2511H%25BFx%25B1%250A%25EDN%25EF

Response Header
HTTP/1.1 200 OK
Date: Thu, 01 Mar 2012 11:20:11 GMT
Server: Apache/2.2.17 (Win32) mod_ssl/2.2.17 OpenSSL/0.9.8o PHP/5.3.4 mod_perl/2.0.4 Perl/v5.10.1
X-Powered-By: PHP/5.3.5
Content-Length: 497
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html

直接转到Chrome中的网址时的标题和响应

Request URL:https://[my company name].basecamphq.com/projects.xml
Request Method:GET
Status Code:304 Not Modified
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,es;q=0.6
Authorization:Basic [**api code that is different from the one i use**]
Cache-Control:max-age=0
Connection:keep-alive
Cookie:[removed]
Host:[my company name].basecamphq.com
If-None-Match:"72b1da3a588429c04fe316d1ab0b047d"
User-Agent:Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.46 Safari/535.11


Response Headers
Cache-Control:max-age=0, private, must-revalidate
Connection:keep-alive
Date:Thu, 01 Mar 2012 10:44:24 GMT
ETag:"72b1da3a588429c04fe316d1ab0b047d"
Server:nginx/1.0.8
Set-Cookie:[removed]
Status:304 Not Modified
Strict-Transport-Security:max-age=31536000
X-Runtime:0.020332
X-Throttle-Count:1
X-Throttle-Horizon:2012-03-01T10:44:30Z
X-Throttle-Max:500
X-UA-Compatible:IE=Edge,chrome=1

0 个答案:

没有答案