使用PHP Ajax查询ssllabs api

时间:2017-09-20 14:16:42

标签: javascript php ajax curl

在可能的情况下获得一般性建议,技术或示例。

我希望将URL的测试与SSLlabs API集成在一起。 www.ssllabs.com/projects/ssllabs-apis/index.html

我已经建立了我需要执行的URL来从这个api中检索信息。

e.g。开始分析 - https://api.ssllabs.com/api/v2/analyze?host=portal.testdomain.co.uk

检索结果 - https://api.ssllabs.com/api/v2/getEndpointData?host=portal.testdomain.co.uk&s=10.111.222.234

我的问题是,我将如何整合到Ajax / php网站? 或者这会更好地仅在后端系统上执行并且结果被泵入DB?

到目前为止,我有一个按钮,它只是抓取包含主机名和IP地址的2个隐藏字段的内容进行查询。

function stateck() 
{
    if(httpxml.readyState==4 && httpxml.status == 200)
    {
    console.log(httpxml.responseText);
    alert(httpxml.responseText);
    }
}


var url="ssltest.php?commonname=" + commonname + "&s=" + ip;
url=url+"&sid="+Math.random();
httpxml.onreadystatechange=stateck;
httpxml.open("GET", url, true);
httpxml.send(null);

ssltest.php包含以下用于查询的cURL函数,然后返回api页面内容。使用url中的参数浏览此ssltest.php可以获得一种享受。

@$commonname=$_GET['commonname'];
@$ip=$_GET['ip'];

if(is_numeric($commonname)){
    echo "Data Error";
    exit;
}

$testurl = "https://api.ssllabs.com/api/v2/analyze?host=" . $commonname;
$resulturl = "https://api.ssllabs.com/api/v2/getEndpointData?host=" . $commonname . "&s=" . $ip;

function ssl_test_start($url,$useragent='cURL',$headers=false,
                        $follow_redirects=false,$debug=false) {
    # initialise the CURL library
    $ch = curl_init();
    # specify the URL to be retrieved
    curl_setopt($ch, CURLOPT_URL,$url);
    # we want to get the contents of the URL and store it in a variable
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    # specify the useragent: this is a required courtesy to site owners
    curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
    # ignore SSL errors
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    # return headers as requested
    if ($headers==true){
        curl_setopt($ch, CURLOPT_HEADER,1);
    }
    # only return headers
    if ($headers=='headers only') {
        curl_setopt($ch, CURLOPT_NOBODY ,1);
    }
    # follow redirects - note this is disabled by default in most PHP installs 
    from 4.4.4 up
        if ($follow_redirects==true) {
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    }
    # if debugging, return an array with CURL's debug info and the URL contents
    if ($debug==true) {
        $result['contents']=curl_exec($ch);
        $result['info']=curl_getinfo($ch);
    }
    # otherwise just return the contents as a variable
    else $result=curl_exec($ch);
    # free resources
    curl_close($ch);
    # send back the data
    return $result;
}

function ssl_test_results($url,$useragent='cURL',$headers=false,
                          $follow_redirects=false,$debug=false) {
    # initialise the CURL library
    $ch = curl_init();
    # specify the URL to be retrieved
    curl_setopt($ch, CURLOPT_URL,$url);
    # we want to get the contents of the URL and store it in a variable
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    # specify the useragent: this is a required courtesy to site owners
    curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
    # ignore SSL errors
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    # return headers as requested
    if ($headers==true){
        curl_setopt($ch, CURLOPT_HEADER,1);
    }
    # only return headers
    if ($headers=='headers only') {
        curl_setopt($ch, CURLOPT_NOBODY ,1);
    }
    # follow redirects - note this is disabled by default in most PHP installs 
    from 4.4.4 up
        if ($follow_redirects==true) {
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    }
    # if debugging, return an array with CURL's debug info and the URL contents
    if ($debug==true) {
        $result['contents']=curl_exec($ch);
        $result['info']=curl_getinfo($ch);
    }
    # otherwise just return the contents as a variable
    else $result=curl_exec($ch);
    # free resources
    curl_close($ch);
    # send back the data
    return $result;
}

if (!isset($ip)){
    $result = ssl_test_start($testurl);
    echo json_encode($result);
}
else {
    $result = ssl_test_start($resulturl);
    echo json_encode($result);
}

然后我如何将$ result返回到原始页面然后访问/显示结果值?我目前正在尝试使用XMLHttpRequest(),但responseText似乎不包含任何内容。

1 个答案:

答案 0 :(得分:0)

最终得到了这个。

简而言之,我使用jQuery GET方法传递给后端php。

$.ajax({
  type: "GET",
  url: "ssltest.php",
  data: dataString,
  dataType: "json",

  success: function(response) {

  //Do something.

  }
})

通过发回的结果数组进行处理并轻松遍历。