Created
December 7, 2012 23:14
-
-
Save sourcec0de/4237402 to your computer and use it in GitHub Desktop.
Cross Origin Resource Sharing with PHP & YII
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// add this to your API controller in Yii | |
public function actionPreflight() { | |
$content_type = 'application/json'; | |
$status = 200; | |
// set the status | |
$status_header = 'HTTP/1.1 ' . $status . ' ' . $this->_getStatusCodeMessage($status); | |
header($status_header); | |
header("Access-Control-Allow-Origin: *"); | |
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE"); | |
header("Access-Control-Allow-Headers: Authorization"); | |
header('Content-type: ' . $content_type); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Where ever the function "_sendResponse" is located | |
// add these lines, this will allow CORS | |
// Allows from any origin | |
// Allows a header called Authorization | |
header("Access-Control-Allow-Origin: *"); | |
header("Access-Control-Allow-Headers: Authorization"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// REST CORS pattern | |
// A preflight request is basically an OPTIONS request to ask for permission to use cross-domain features. | |
// So we have add the proper verb in the url manager rules (config/main.php): | |
array('api/preflight', 'pattern'=>'api/*', 'verb'=>'OPTIONS'), |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot I was stuck in this problem for 2 days. God bless you