Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Heimdall's avatar

Curl

Hello,

I have a problem with my curl :

I connect to a page with CURL. then I redirect myself to a page that is available when you are connected. and then I would like to retrieve information on this page. But how can I do to get the html code? I have the page that is displayed, but I can't get the source code for scrap

My connexion


$data = array('hardkey'=>'obligatoire',
              'login'=>'xxx',
              'password'=>'xxxx',
              'x'=>'41',
              'y'=>'14'
            );

$value = http_build_query($data) . "\n";

$url = 'https://www.mywebsite.com/login.php';

    //open connection
    $ch = curl_init();

    //set the url, number of POST vars, POST data
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_POSTFIELDS, $value);
    curl_setopt($ch,CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch,CURLOPT_COOKIEJAR, "cookie.txt");
    curl_setopt($ch,CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);

//curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
    //execute post
    $result = curl_exec($ch);


My redirection :


    $url = "https://www.mywebsite.com/porte_transferer.php";
curl_setopt ($ch, CURLOPT_POST, 0); 
curl_setopt($ch, CURLOPT_URL, $url);

$result = curl_exec($ch);

with that, i see the page, but idk how recover html for scrap my information

Thank for your help

0 likes
8 replies
idew's avatar
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

should do the trick.

Heimdall's avatar

@idew i tried but that dont work :/ currently i have a other problem, i want recover html response with curl

sr57's avatar

i see the page, but ... how recover html ...

Not clear, if you see the page, you get the html !

Can you share an image of what you see and the code you get in $result

Heimdall's avatar

Ok, @sr57

$valueRessource = http_build_query($dataRessource) . "\n";

$url = "https://www.mywebesite.fr/univers-karan/porte_transferer.php";


//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $valueRessource);
curl_setopt(
    $ch,
    CURLOPT_USERAGENT,
    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

$result = curl_exec($ch);

with this code i see the page. But when i echo $result or vardump my result is 1 .....

Heimdall's avatar

I m really bloqued :

my problem and my code:

This is for login to the website

$data = [
    "hardkey" => "obligatoire",
    "login" => "xxx",
    "password" => "xx",
    "x" => "41",
    "y" => "14",
];

$value = http_build_query($data);

$url = "https://www.xxx.fr/univers-karan/login.php";

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $value);
curl_setopt(
    $ch,
    CURLOPT_USERAGENT,
    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIESESSION, true);


$result = curl_exec($ch);

After i go at the page porte_transferer

$fp = fopen("example_homepage.txt", "w");

$url = "https://www.origins-return.fr/univers-karan/porte_transferer.php";
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

$result = curl_exec($ch);


if (curl_error($ch)) {
    fwrite($fp, curl_error($ch));
}
fclose($fp);


// fin de la redirection


// creation du dom pour trouver les informations

$myfile = fopen("example_homepage.txt", "r") or die("Unable to open file!");
$test = fread($myfile, filesize("example_homepage.txt"));

$dom = new simple_html_dom();

// Load HTML from a string

$html = $dom->load($test);

$id = "";
$i = 0;
foreach ($html->find("input") as $element) {
    if ($i == 2) {
        $id = $element->value;
    }
    $i++;
}

after this, i send the form at porte_transferer.php

$dataRessource = [
    "planete" => $id,
    "exe" => "1",
    "galaxi" => "1",
    "system" => "87",
    "position" => "4",
    "fer_ressource" => "10.000",
    "or_ressource" => "0",
    "cristal_ressource" => "0",
    "ura_ressource" => "0",
    "eppz" => "0",
    "mili_nbre" => "0",
    "malp" => "0",
    "sonde" => "0",
    "defdrone" => "0",
    "ptremorqueur" => "0",
    "gdremorqueur" => "0",
    "megaremorqueur" => "0",
    "defions" => "0",
    "defanciens" => "0",
    "deforis" => "0",
];

$valueRessource = http_build_query($dataRessource) . "\n";

$url = "https://www.origins-return.fr/univers-karan/porte_transferer.php";


//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $valueRessource);
curl_setopt(
    $ch,
    CURLOPT_USERAGENT,
    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

$result = curl_exec($ch);


$i =0;

and this send me to validate form on porte_transferer.php, and i want recover html for take hidden value to send form and validate

if i m not clear, i can ask your question

Heimdall's avatar

i take html with jquery like this:

<script> 
     var content = $("body").html();
               $.post("infopagetransfert2.php", {
                  content: content,
               }).done(function (data) {
               });
</script>
include_once "simple/simple_html_dom.php";
session_start(); 
$content = $_POST["content"];
$fp = fopen("example_homepage2.txt", "w");
fwrite($fp, $content);
fclose($fp);


$myfile = fopen("example_homepage2.txt", "r") or die("Unable to open file!");
$test = fread($myfile, filesize("example_homepage2.txt"));

$dom = new simple_html_dom();

// Load HTML from a string

$html = $dom->load($test);

$id = "";
$i = 0;
foreach ($html->find("input") as $element) {
    if ($i == 3) {
        $id = $element->value;
    }
    $i++;
}

$_SESSION["newsession"] = $id; 



$dataRessource = [
    "confirm" => "1",
    "planete" =>   $_SESSION['newsession'],
    "exe" => "1",
    "galaxi" => "1",
    "system" => "87",
    "position" => "4",
    "fer_ressource" => "10.000",
    "or_ressource" => "0",
    "cristal_ressource" => "0",
    "ura_ressource" => "0",
    "eppz" => "0",
    "mili_nbre" => "0",
    "malp" => "0",
    "sonde" => "0",
    "defdrone" => "0",
    "ptremorqueur" => "0",
    "gdremorqueur" => "0",
    "megaremorqueur" => "0",
    "defions" => "0",
    "defanciens" => "0",
    "deforis" => "0",
    "ada_ressource" => "0",
];

$valueRessource = http_build_query($dataRessource) . "\n";

$ch = curl_init();
$url = "https://www.xxxx/univers-karan/porte_transferer.php";

//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $valueRessource);
curl_setopt(
    $ch,
    CURLOPT_USERAGENT,
    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT,10);


$result = curl_exec($ch);

but that dont work ...

Please or to participate in this conversation.