【问题标题】:Using PHP Ajax to query ssllabs api使用 PHP Ajax 查询 ssllabs api
【发布时间】:2017-09-20 14:16:42
【问题描述】:

如果可能,在一般建议、技术或示例之后。

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

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

例如开始分析-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 网站? 还是仅在后端系统上执行并将结果泵入数据库会更好?

到目前为止,我有一个按钮,可以简单地抓取包含要查询的主机名和 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 似乎不包含任何内容。

【问题讨论】:

  • "仅在后端系统上执行并将结果输入数据库会更好吗?"取决于您现在或以后想要结果。但是,为什么您不能直接向 API 发出 ajax 请求而无需中间的 PHP 脚本呢?不支持CORS吗?
  • 更多挖掘表明我想要完成的工作不在 SSLLabs 的使用条款范围内。和 CORS 请求被阻止。 community.qualys.com/thread/… 。所以看起来我只能用后端脚本来做到这一点。
  • responseText 只是旅程的终点​​。您需要直接回溯代码以找出出错的地方。在某些时候,您要么没有正确发送请求,要么没有得到正确的响应。使用 Fiddler 之类的工具来跟踪当您向 PHP 发出 ajax 请求,然后向 SSLLabs 发出请求时实际发生的情况,并查看请求和响应的样子。如果一切正常,那么您需要调试您的代码流(例如通过跟踪变量等)。
  • 如果对 SSLLabs 的请求不正常,则将其与浏览器请求进行比较,看看有什么区别 - 也许您需要更改 curl 等中的选项。如果是您的 ajax 请求有问题,然后发布您在问题中遇到的错误/问题,我们可以查看它。 HTTP 状态代码,浏览器控制台中的任何错误响应,诸如此类。粗略一看,您的代码中没有明显错误,所以它可能是某处的一些细节。
  • 我唯一要说的是,我不知道 commonnameip 的内容(尽管我可以猜到后者!)而且你不是 URL-对它们进行编码,因此如果其中有任何文本可以被视为 URL 或查询字符串中的特殊字符,或者它们包含空格等,那么这可能会导致请求失败或行为异常。

标签: javascript php ajax curl


【解决方案1】:

最终解决了这个问题。

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

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

  success: function(response) {

  //Do something.

  }
})

工作一种享受,并且易于遍历返回的结果数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-20
    • 2014-05-24
    相关资源
    最近更新 更多