簡體   English   中英

php cURL實例化遠程類

[英]php cURL instantiate remote class

cURL的新增功能,並嘗試加載PHP文件以使用以下代碼實例化一個類。

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$n = new Test_cURL();

我已經交叉檢查並啟用了cURL。 還通過遠程URL對該文件進行交叉檢查

if (curl_exec($ch) !== FALSE) {
    return true;
} 
else {
    return false;
}

但是,當我創建新實例時,它給了我錯誤。

Fatal error: Class 'Test_cURL' not found in

因此,如何加載文件以允許從遠程實例化類?

更新

所有必需的詳細信息,包括網站URL和文件名

<?php
/**
 * Remote Class
 * URL: http://localhost/php-lib/lib.php
 * filename: lib.php
 */
class Test_cURL
{
    public $msg;
    function __construct($message)
    {
        $this->msg = $message;
    }

    public function f_msg($add)
    {
        return $this->msg . ' is the property and ' . $add . ' is the parameter!';
    }
}


/**
 * cURL file on other site (could be other server)
 * URL: http://test-site/index.php
 * Filename: index.php
 */
$url = "http://localhost/php-lib/lib.php";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$result=curl_exec($ch);
eval($result);

$n = new Test_cURL('loader message');
echo $n->f_msg('method message');

/**
 * Error message
 */
Fatal error: Class 'Test_cURL' not found in /var/www/html/test-site/index.php on line 26

您可以使用file_get_contents然后使用file_put_contents在本地存儲文件。

include文件並實例化該類。

參考: http : //blog.kotowicz.net/2010/07/re-hardening-php-how-to-securely.html

使用eval()來運行您的類,並且CURLOPT_NOBODY應該為false以便它可以返回string

卷曲代碼:

<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '$url');
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$result=curl_exec($ch);

eval($result);



$n = new Test_cURL();

echo $n->name;

班級代碼:

<?php

echo '
class Test_cURL{

public $name="rasmus lerdorf";

}';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM