I have a issue with CORS in php

cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Active Member II

I have a issue with CORS in php

Jump to solution

Hi, 

I have a issue when i consume the Alfresco APIs.

I have installing Alfresco Version 5.2.0 (201707).

I want consume the Alfresco APIs with PHP. I have a code in php i use JQUERY for that. I have used JQUERY, AJAX, AXION but but none works. In console i have this issue. "Access to XMLHttpRequest at 'http://localhost:8080/alfresco/service/api/login.json?u=admin&pw=admin' from origin 'http://localhost' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource." 

I don't know if there is an SDK for PHP. I have used some SDK for PHP but is very OLD. Also i have added the permissions in the head and it doesn't work for me either.

This is my code. Is very simple! 

<?php

if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400');
}

if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");

if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
} ?>


<html>

<head>
<script type = "text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<script type = "text/javascript" language = "javascript">

$(document).ready(function() {
var surl = "http://localhost:8080/alfresco/service/api/login.json?u=admin&pw=admin";
$.ajax({
url: surl,
dataType: "json",
success: function (data) {
alert(JSON.stringify(data));
}
});
});

</script>
</head>

<body>
</body>

</html>

I hope someone can help me. Thank you.

  

1 Solution

Accepted Solutions
Highlighted
Moderator
Moderator

Re: I have a issue with CORS in php

Jump to solution
6 Replies
Highlighted
Moderator
Moderator

Re: I have a issue with CORS in php

Jump to solution
Highlighted
Active Member II

Re: I have a issue with CORS in php

Jump to solution

Hi abhinavmishra

I was very helpful. I am very grateful.  By last. Are there any codes made with PHP that I can try with these APIs?

 

Highlighted
Moderator
Moderator
Highlighted
Active Member II

Re: I have a issue with CORS in php

Jump to solution

Thank you.

I see that many links do not work. And I have urged to install that SDK but the instructions link does not work. Do you have these links on another site working? 

Highlighted
Moderator
Moderator

Re: I have a issue with CORS in php

Jump to solution

Unfortunately i don't have any other info. You can take the reference from the tutorial. Or try contacting the project owner on github. Its very old project though

~Abhinav
(ACSCE, AWS SAA-C02, GAIQ)
Highlighted
Active Member II

Re: I have a issue with CORS in php

Jump to solution

Ok.. Thank you very much..