【发布时间】:2014-08-15 04:22:37
【问题描述】:
对于 PHP 项目,我一直想根据用户提供的反馈向 Github 项目添加问题。但是,我只是从 Github API 中获取一些东西时遇到了困难。以下命令行为我提供了我希望获得的信息:
$ curl -u <username> https://api.github.com/user
这给了我我的用户信息,但是当我尝试使用 PHP 时,我只是得到黑页。我的代码是:
<?php
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, 'https://api.github.com/user' );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_USERPWD, "<username>:<password>" );
curl_setopt( $ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
$output = curl_exec( $ch );
$info = curl_getinfo( $ch );
curl_close( $ch );
echo $output;
var_dump( $info );
?>
我怀疑 $output 与我在命令行中得到的相同,但这里它是空白的。我得到的是 $info:
array(26) { ["url"]=> string(27) "https://api.github.com/user" ["content_type"]=> NULL ["http_code"]=> int(0) ["header_size"]=> int(0) ["request_size"]=> int(0) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(0.203) ["namelookup_time"]=> float(0.015) ["connect_time"]=> float(0.109) ["pretransfer_time"]=> float(0) ["size_upload"]=> float(0) ["size_download"]=> float(0) ["speed_download"]=> float(0) ["speed_upload"]=> float(0) ["download_content_length"]=> float(-1) ["upload_content_length"]=> float(-1) ["starttransfer_time"]=> float(0) ["redirect_time"]=> float(0) ["redirect_url"]=> string(0) "" ["primary_ip"]=> string(14) "192.30.252.136" ["certinfo"]=> array(0) { } ["primary_port"]=> int(443) ["local_ip"]=> string(13) "192.168.0.109" ["local_port"]=> int(28935) }
我找不到在 PHP 中使用的任何简单示例,而且我不想将整个框架用于应该如此简单的事情。根据我从 API 文档中获得的信息,我应该能够发送我的凭据,然后获取信息。
我可能遗漏了一些明显的东西,但无法弄清楚,谁能指出我正确的方向?
【问题讨论】:
-
我一直在寻找并发现this 问题。除非我包含
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );,否则在本地工作时,似乎 curl 对于 https 返回为空。之后,我收到消息我缺少用户代理,并且在包含它之后它可以工作。希望我能帮助其他人。 -
请考虑将其提交为您自己问题的答案,然后接受它。这样,其他人就有更好的机会从您的工作中学习。
-
我会的,我以前做不到