Assuming you already know the ajax request flow (because you have tried jsonp), you can make a "proxy" system where you would make the ajax request to a specific file on your server and that file would make the request via Curl or file_get_contents to the desired site to circumvent CORS. the flow would be more or less like this.
- User enters the abc page
- The page is redeveloped in the browser
- After x seconds, a js on the abc page does ajax to the xpto.php file on the same server.
- xpto.php file makes Curl, prepares the data to return to abc
- abc page receives the data and makes business logic
In practice the implemented flow would be more or less like this:
Página abc (only the ajax part with jQuery)
jQuery(document).ready(function ($) {
setTimeout(function () {
$.get('xpto.php', function (dados) {
alert('Dados recebidos, veja o console para mais detalhes');
console.log(dados);
// fazer alguma coisa coms os dados
});
}, 50000); // 50 segundos
});
Filename foo.php
<?php
$fields = array('site' => $site, 'uid' => $uid, 'ip' => $ip, 'id' => $id);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://xxxx.xx/widget-ajax.php');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
// Talvez você precise desativar a verificação de peers localmente
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
curl_close($ch);
// To Do não esquecer de tratar a respota antes de devolvê-la
echo json_encode(['data' => $result]);
exit;
If you need to always be doing the checking from time to time just change the function of setTimeout
for setInterval
If you prefer a PHP only side approach you try one of the solutions proposed in this article (in English)
This does not work, it paralyzes the whole page script!
– user93341
@Viníciusremonti I edited my answer with an example as an alternative to jsonp that avoids the CORS problem
– Vinicius.Silva