Access-Control-Allow-Origin - php

Рейтинг: -2Ответов: 1Опубликовано: 20.10.2014

Вот скрипт:

1) a.php

header('Access-Control-Allow-Origin: *');

include_once "./q.php";

$res = new MyClass($_GET['q']);

print $res->data;

2) q.php

class MyClass {

    public function MyMethod () {

        $get = file_get_contents("http://sdfdsfsdfdsfsd");
        $decode = json_decode($get, TRUE); // TRUE for in array format

        foreach($decode['SearchResponse']['Web']['Results'] as $res)
        {
            echo('<div id="res">');
            echo "<p><div id='desc'>".$res['Description']." - <a href='".$res['Url']."' rel='nofollow'  target='_blank'>read more</a></div></p>";
            echo('</div>');
        }

    }
}

При обращение к скрипту a.php выдает ошибку:

[20-Oct-2014 19:29:09 Europe/Helsinki] PHP Notice: Undefined property: MyClass::$data in

Ругается на эту строку:

print $res->data;

Как исправить?

Ответы

▲ 1Принят

Как-то вот так, подробностей вопроса я не увидел:

class MyClass {

    public $data;

    function __construct ($data) {
        $this->data = $data;
    }

    public function MyMethod () {

        // тут твой код;

    }

}