2011-11-14 21 views

回答

5

對於任何人來到這個網頁從谷歌上搜索,我提出一個問題,在github上,並得到了從作者的大力支持。如果您熟悉Restler的構建過程,那麼實現它就相當簡單了。

https://github.com/Luracast/Restler/issues/17

<?php 
//jsonpformat.php 
class JsonpFormat implements iFormat { 

    const MIME = 'text/javascript'; 
    const EXTENSION = 'js'; 
    /* 
    * JsonFormat is used internally 
    * @var JsonFormat; 
    */ 
    public $jsonFormat; 
    public static $functionName = 'parseResponse'; 

    public function __construct() { 
     $this->jsonFormat = new JsonFormat(); 
     if (isset ($_GET ['jsonp'])) { 
      self::$functionName = $_GET ['jsonp']; 
     } 
    } 
    public function getMIMEMap() { 
     return array (self::EXTENSION => self::MIME); 
    } 
    public function getMIME() { 
     return self::MIME; 
    } 
    public function getExtension() { 
     return self::EXTENSION; 
    } 
    public function encode($data, $human_readable = FALSE) { 
     return self::$functionName . '(' . $this->jsonFormat->encode ($data, $human_readable) . ');'; 
    } 
    public function decode($data) { 
     return $this->jsonFormat->decode ($data); 
    } 
    public function setMIME($mime) { 
     //do nothing 
    } 
    public function setExtension($extension) { 
     //do nothing 
    } 
} 
?> 

這應該被保存在同一目錄下restler.php文件。一旦你有了,編輯你的網關(index.php)來包含這個文件,並將它添加爲支持的格式。例如:

<?php 
require_once '../../restler/restler.php'; 

#set autoloader 
#do not use spl_autoload_register with out parameter 
#it will disable the autoloading of formats 
spl_autoload_register('spl_autoload'); 

$r = new Restler(); 
$r->setSupportedFormats('JsonpFormat','JsonFormat', 'XmlFormat'); 
$r->addAPIClass('BMI'); 
$r->handle(); 
?> 
+0

不錯。我想知道,如果這是安全的:self :: $ _ GET ['jsonp']? –

+0

黑客的框架是一個壞主意......這一個更好...... – danielrvt

+0

不應該Luracast允許我們設置一個'Defaults :: $ accessControlAllowHeaders'靜態成員類型處理? – Miles

1

這很適合於我們: 頭( '訪問控制允許來源:*');

到控制器方法返回單個終點在此之前加入到控制器類構造函數中的一個分支,所有端點或上漲,以允許其網站廣泛。

如果您只允許某些網站訪問使用標題('Access-Control-Allow-Origin:example.com')或類似標題('Access-Control-Allow-Origin:'。$ remote_domain)。其中$ remote_domain是基於某些傳入的令牌或其他動態設置的。查看跨源資源共享(CORS),瞭解爲什麼限制*通配符的使用。

<?php 

class Say { 

__construct(){ 
    header('Access-Control-Allow-Origin: *'); //Here for all /say 
} 

function hello($to='world') { 
    header('Access-Control-Allow-Origin: *'); //Here for just /say/hello 
    return "Hello $to!"; 
    } 
} 

上述工作適用於GET和POST,其他操作需要一些來自restler的額外頭信息。下面是一些例子:

報頭( '訪問控制允許的方法:GET,POST,DELETE,PUT,OPTIONS'); header('Access-Control-Allow-Headers:whatever_headers_you_allow,header1,header2');

對於IE9和下面你將需要一個JSONP黑客。 Restler具有擴展iFormat類以包裝API輸出JASONP風格的示例。

查看Mozilla黑客獲取CORS的詳細信息。 http://hacks.mozilla.org/2009/07/cross-site-xmlhttprequest-with-cors/ 並檢查了OPTIONS in PHP REST API

+0

上面的作品很適合GET和POST,但其他操作需要一些作品。查看Mozilla黑客的一些細節。 HTTP://hacks.mozilla。組織/ 2009/07 /跨站點XMLHttpRequest的-與-CORS / – punkael

0

在這裏我要補充一點,如果由於某種原因你不想使用JSONP,你可以簡單地添加:

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

每punkael的第一個答案(他沒有在Rester中指定在何處執行此操作)。將此行添加到sendData($ data)函數中的restler.php,其中Restler將頭數據添加到響應中。這將啓動上線378

保重然而,因爲這將允許任何域從您的API獲取數據。

相關問題