How to update Curl request when calling script

Asked

Viewed 544 times

0

I have a system, which opens an external site inside it, only I have a small obstacle; every time the page is accessed for the first time, the data obtained from the external page are not updated, they are cached and stay forever, wanted to update this whole data made the page call the script responsible for opening the external page.

I pass the dice by Javascript to the archive PHP, the javascript picks up the url of the news on the site, and send through the parameter GET to the script PHP, that carries out the opening of the site making use of the library cURL. The Script PHP is like this:

if(preg_match("#Politica#",$_GET['u'])){
    $ir = $_GET['u'];
    ob_start();
    $cot = new ExibirPolitica();
    $cot -> setUrlc($ir);
    $cot -> printCot();
    $conteudo = ob_get_contents();
    ob_end_clean();
    echo $conteudo;

}

The Class ExibirPolitica(); contains only one Curl as follows:

        $header = "X-Forwarded-For: {$_SERVER['REMOTE_ADDR']}";
        $h2 = "Cache-Control: no-cache";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_REFERER, "https://noticiando.com");
        curl_setopt($ch, CURLOPT_HTTPHEADER, array($header,$h2));
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
        $html = curl_exec($ch);

As you can see, I tried to use the ob_end_clean(); and print the content below to see if it came updated, also tried the "no-cache" in the cURL, also unsuccessful!

Is there any way to "force" the update of a content obtained by cURL?

1 answer

1


I don’t know how your URL is formatted, but I think you have some variables in it. So I have two suggestions.

// Em ambos casos, você precisará da variável com valor pseudo-exclusivo
// A chance do valor dessa variável se repetir é praticamente nula
// Foi dado o nome de "_u" mas pode ter qualquer outro nome
$u = '_u=' . microtime(true) . ':' . rand();

Seg 1: Continue using the GET method and add the variable at the end of the URL:

$u = ((strpos($url, '?') === false) ? '?' : '&') . $u;
curl_setopt($ch, CURLOPT_URL, $url . $u);

Seg 2: Use the POST method and send the variable this way:

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $u);

I believe these two suggestions will work, as both produce a virtually exclusive request.

  • Thank you Rodrigo! gave right to suggestion 1.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.