I have a issue with CORS in php

cancel
Showing results for 
Search instead for 
Did you mean: 
viperboys
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
abhinavmishra14
Advanced

Re: I have a issue with CORS in php

Jump to solution

Follow this document and configure cors on your installation: https://docs.alfresco.com/5.2/tasks/enable-cors.html

You can also look at this post, its not with PHP though but may give some hints: https://hub.alfresco.com/t5/application-development/adf-cors-solving-strategies/bc-p/286963

Some additonal posts related to CORS:

http://anshu-alfresco-one.blogspot.com/2015/08/enable-cors-in-alfresco-and-how-to-use.html

https://hub.alfresco.com/t5/application-development/enabling-cors/td-p/218640

~Abhinav
(ACSCE, AWS SAA, Azure Admin)

View solution in original post

6 Replies
abhinavmishra14
Advanced

Re: I have a issue with CORS in php

Jump to solution

Follow this document and configure cors on your installation: https://docs.alfresco.com/5.2/tasks/enable-cors.html

You can also look at this post, its not with PHP though but may give some hints: https://hub.alfresco.com/t5/application-development/adf-cors-solving-strategies/bc-p/286963

Some additonal posts related to CORS:

http://anshu-alfresco-one.blogspot.com/2015/08/enable-cors-in-alfresco-and-how-to-use.html

https://hub.alfresco.com/t5/application-development/enabling-cors/td-p/218640

~Abhinav
(ACSCE, AWS SAA, Azure Admin)
viperboys
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?

 

abhinavmishra14
Advanced
viperboys
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? 

abhinavmishra14
Advanced

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, Azure Admin)
viperboys
Active Member II

Re: I have a issue with CORS in php

Jump to solution

Ok.. Thank you very much..