PHP获取私有属性

发布于 2022-09-04 14:57:15 字数 1088 浏览 20 评论 0

调用阿里的SDK,返回如下数据:

HttpResponse Object
(
    [body:HttpResponse:private] => {
        "RequestId":"A7847F5F-959A-442F-9D4F-D9A823857128","AssumedRoleUser":{"AssumedRoleId":"381320982304412425:704036879","Arn":"acs:ram::20411481:role/bfvideo/704036879"},
        "Credentials":{"AccessKeySecret":"BDsQMcruMnuP9a1qDJYYGUCPorjwyhBTJ7bUrF7vLoua",
        "AccessKeyId":"STS.Bg5E16x7SzBwg5yw13Wj4W5tW","Expiration":"2016-12-22T06:14:01Z",
        "SecurityToken":"CAIS8AF1q6Ft5B2yfSjIo6SADouClehyzYCcZRPIkzFmW+UYuPDftTz2IHBNf3NoCOActfwzmGlS6vwflqAsE8MdHhKUMpoocQmXMPniMeT7oMWQweEut//MQBqpaXPS2MvVfJ+5Lrf0ceusbFbpjzJ6xaCAGxypQ12iN+/x6/h8cs9FdxKjcD9LPtBSK3EVyqkgOGDWKOymPzPzn2PUFzAIgAdnjn5l4qnNqa/1qDi+1gWmk7ZJ993LT8L6P5U2DvBWSMyo2eF6TK3F3RNL5gJCnKUM1/wdom2f74HHWAENv0zcb7CJ6LJlIhF4aqU9Cx/EGZkagAFmPDKrgHtOyVvo4LQk/dVWyukdXLbSfrnHFSd0vY6RQMLdujjvG1QPH4x7Yw1XemtVIepth51kZbUtgVIKN8TkGz/MlgGCM7iEJg5dwVdenn2b7XEmUwKs8rw3shXeAIydSFfiOqteruPzqBkR3R02jF/Btagf4la4HbuMSd8FMg=="}
    }
    [status:HttpResponse:private] => 200
)

这些是私有属性,请问我如何获取到这些值呢?
谢谢诸位

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

缪败 2022-09-11 14:57:15

你看看它提供的应该有

转array/json

或者 getter之类的方法吧

丢了幸福的猪 2022-09-11 14:57:15

既然是PHP,改它的源代码啊, private -> public

如果改不动的话

获取私有属性,调用私有方法

如下:
最后那个用了php 5.6.8+的特性

/**
 * set class's public/private/protected property
 *
 * @param object $class
 * @param string $variant property name
 * @param string $value value
 *
 * @return array
 */
if (!function_exists('set_property'))
{
function set_property($class, $variant, $value)
{
    if (!is_object($class)) throw new Exception('paramater #0 must be an object\'s instance.', 1);

    $property = (new ReflectionClass($class))->getProperty($variant);
    $property->setAccessible(true);

    return $property->setValue($class, $value);
}
}
/**
 * get class's public/private/protected property
 *
 * @param object $class
 * @param string $variant property name
 *
 * @return array
 */
if (!function_exists('get_property'))
{
function get_property($class, $variant)
{
    if (!is_object($class)) throw new Exception('paramater #0 must be an object\'s instance.', 1);
    
    $property = (new ReflectionClass($class))->getProperty($variant);
    $property->setAccessible(true);

    return $property->getValue($class);
}
}

/**
 * call class's public/private/protected method
 *
 * @param object $class
 * @param string $variant property name
 * @param string $value value
 *
 * @return array
 */
if (!function_exists('call_class_method_array'))
{
function call_class_method_array($class, $method, $parameters)
{
    if (!is_object($class)) throw new Exception('paramater #0 must be an object\'s instance.', 1);

    $reflectionMethod = (new ReflectionClass($class))->getMethod($method);
    $reflectionMethod->setAccessible(true);

    return $reflectionMethod->invokeArgs($class, $parameters);
}
}

/**
 * call class's public/private/protected method
 *
 * @param object $class
 * @param string $variant property name
 * @param string $value value
 *
 * @return array
 */
if (!function_exists('call_class_method'))
{
function call_class_method($class, $method, ...$parameters)
{
    if (!is_object($class)) throw new Exception('paramater #0 must be an object\'s instance.', 1);

    $reflectionMethod = (new ReflectionClass($class))->getMethod($method);
    $reflectionMethod->setAccessible(true);

    return $reflectionMethod->invokeArgs($class, $parameters);
}
}
昨迟人 2022-09-11 14:57:15

私有属性是不能直接访问的

谁与争疯 2022-09-11 14:57:15

这是阿里云sdk接口的问题,你直接改动他的sdk,返回数据就是json格式了。改动此目录下,aliyun-openapi-php-sdk-masteraliyun-openapi-php-sdk-masteraliyun-php-sdk-coreHttp,的HttpHelper.php,大概37行,删除或者屏蔽curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);即可。然后json数据就直接就返回了,无需再print_r($rsponse).

一梦等七年七年为一梦 2022-09-11 14:57:15

面向对象调用
阿里云sdk的代码HttpHelper.php
$httpResponse->getBody();
$httpResponse->getStatus();

class HttpHelper
{
    public static $connectTimeout = 30000;//30 second
    public static $readTimeout = 80000;//80 second
    
    public static function curl($url, $httpMethod = "GET", $postFields = null,$headers = null)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpMethod); 
        if(ENABLE_HTTP_PROXY) {
            curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); 
            curl_setopt($ch, CURLOPT_PROXY, HTTP_PROXY_IP); 
            curl_setopt($ch, CURLOPT_PROXYPORT, HTTP_PROXY_PORT);
            curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); 
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FAILONERROR, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($postFields) ? http_build_query($postFields) : $postFields);
        
        if (self::$readTimeout) {
            curl_setopt($ch, CURLOPT_TIMEOUT, self::$readTimeout);
        }
        if (self::$connectTimeout) {
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$connectTimeout);
        }
        //https request
        if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        if (is_array($headers) && 0 < count($headers))
        {
            $httpHeaders =self::getHttpHearders($headers);
            curl_setopt($ch,CURLOPT_HTTPHEADER,$httpHeaders);
        }
        $httpResponse = new HttpResponse();
        $httpResponse->setBody(curl_exec($ch));
        $httpResponse->setStatus(curl_getinfo($ch, CURLINFO_HTTP_CODE));
        if (curl_errno($ch))
        {
            throw new ClientException("Speicified endpoint or uri is not valid.", "SDK.ServerUnreachable");
        }
        curl_close($ch);
        return $httpResponse;
    }
    
    static function getHttpHearders($headers)
    {
        $httpHeader = array();
        foreach ($headers as $key => $value)
        {
            array_push($httpHeader, $key.":".$value);    
        }
        return $httpHeader;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文