【问题标题】:PHP to get password-protected REST datasnap informationPHP 获取受密码保护的 REST 数据快照信息
【发布时间】:2016-04-25 00:43:57
【问题描述】:

我有以下代码从 DataSnap 服务器获取一些信息:

<?php
    $username = USERNAME
    $password = PASSWORD
    $service_url = 'http://200.206.17.34:81/datasnap/rest/TServerMethods1/blogin/';
    $curl = curl_init($service_url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
    $curl_response = curl_exec($curl);
    echo $curl_response;
    if ($curl_response === false) {
        $info = curl_getinfo($curl);
        curl_close($curl);
        die('Error occured during curl exec. Additional info: ' . var_export($info));
    }
    curl_close($curl);
    $decoded = json_decode($curl_response);
    if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
        die('Error occured: ' . $decoded->response->errormessage);
    }
    echo 'Response ok!';
    var_export($decoded->response);
?>

当我运行它时,我得到的是:

array ( 'url' => 'http://200.206.17.34:81/datasnap/rest/TServerMethods1/blogin/', 'content_type' => NULL, 'http_code' => 0, 'header_size' => 0, 'request_size' => 0, 'filetime' => -1, 'ssl_verify_result' => 0, 'redirect_count' => 0, 'total_time' => 0, 'namelookup_time' => 0.0002249999999999999938417316602823348148376680910587310791015625, 'connect_time' => 0, 'pretransfer_time' => 0, 'size_upload' => 0, 'size_download' => 0, 'speed_download' => 0, 'speed_upload' => 0, 'download_content_length' => -1, 'upload_content_length' => -1, 'starttransfer_time' => 0, 'redirect_time' => 0, 'redirect_url' => '', 'primary_ip' => '200.206.17.34', 'certinfo' => array ( ), )Error occured during curl exec. Additional info:

我做错了什么?

--编辑:

我可以使用浏览器和 wget 连接到该地址。

【问题讨论】:

    标签: php rest curl datasnap


    【解决方案1】:

    来自 curl_info 的数据表明连接到服务器有问题,例如,“'content_type' => NULL, 'http_code' => 0”等。

    尝试在关闭第一个if 语句中的$curl 处理程序之前插入print curl_error($curl) 以获取更多信息。

    【讨论】:

    • 我收到couldn't connect to hostarray
    • 主机的81端口是否开放?它似乎是一所大学,默认情况下,他们的安全策略通常是相当严格的。另外,请尝试使用curl_setopt($curl, CURLOPT_PORT, '81'); 而不是通过$service_url 变量设置端口号。
    • 我可以从我的浏览器连接到它,它是 PHP 不能,这样做也没有用。
    • 嗯..可能是您的服务器配置。您可以使用 wget 和您的登录凭据通过命令行连接吗? wget --user user --password pass http://serveraddress:81 对于 unix;对于 Windows,将 : 替换为 +
    • 几乎可以肯定是身份验证错误。尝试显式设置 CURLAUTH_BASIC 而不是任何身份验证方法。如果您使用代理,您可能也必须考虑到这一点。
    猜你喜欢
    • 2014-07-07
    • 2011-07-22
    • 2022-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多