用PHP表示GitHub JSON API

时间:2017-09-13 09:33:50

标签: php json

我目前正在编写PHP脚本,通过GitHub API(https://api.github.com/users/{username}/events)查找用户的GitHub电子邮件地址。

我已经考虑过这样做的方法,但是我很难将它实现到PHP中。我的思维过程是拉出JSON,搜索“电子邮件”。 string(使用循环和一些RegEx)然后返回结果。

到目前为止这是我的PHP(我还是该语言的新手): https://hastebin.com/wuzezotuqi.xml

1 个答案:

答案 0 :(得分:0)

您可以在客户端浏览器中使用 javascript 来抓取电子邮件。

//parse the response for the email.
function reqListener () {
  var para = document.getElementById('textpad');
    var emailIndex = this.responseText.indexOf("email");

    var nextColonIndex = this.responseText.indexOf(":", emailIndex);
    var nextCommanIndex = this.responseText.indexOf(",", emailIndex);

    var emailAddr = this.responseText.substr(nextColonIndex + 1, nextCommanIndex-nextColonIndex - 1);
}


//
// making the api request
//
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", reqListener);
oReq.open("GET", "https://api.github.com/users/ratulSharker/events");
oReq.send();

检查jsfiddle

中的操作

如果您打算使用 PHP

在服务器端执行此操作
<?php

$config['useragent'] = 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0';

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $config['useragent']);
curl_setopt($ch, CURLOPT_URL, 'https://api.github.com/users/ratulSharker/events');
        $content = curl_exec($ch);



$emailIndex = strpos($content, "email", 0);     // find from the start


$nextColonIndex = strpos($content, ":", $emailIndex);
$nextCommanIndex = strpos($content, "," , $emailIndex);
$nextQuoteIndex = strpos($content, "\"", $nextColonIndex);
$nextNextQuoteIndex = strpos($content, "\"", $nextQuoteIndex+1);



    $emailAddr =  substr($content, $nextQuoteIndex, $nextNextQuoteIndex-$nextQuoteIndex+1);

echo $emailAddr;

检查PHPFiddle

中的代码