Skip to content

Instantly share code, notes, and snippets.

@sourcec0de
Created December 7, 2012 23:14
Show Gist options
  • Save sourcec0de/4237402 to your computer and use it in GitHub Desktop.
Save sourcec0de/4237402 to your computer and use it in GitHub Desktop.
Cross Origin Resource Sharing with PHP & YII
// 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);
}
// 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");
// 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'),
@usman10scorpio
Copy link

Thanks a lot I was stuck in this problem for 2 days. God bless you

@steven-work-space
Copy link

Thanks a lot !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment