Jump to content

[SOLVED] Exploding cURL source


Thomisback

Recommended Posts

Hi,

 

I have made a PHP page which loads a page by using cURL. Now I want to extract a specific piece of text from the source but for some reason I can't do it.

My code:



<?php

set_time_limit(0);


$ch = curl_init("Link.example");
$usecookie = "cookie.txt"; 
       curl_setopt($ch, CURLOPT_COOKIEJAR, $usecookie);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $usecookie);    
$bestand = curl_exec($ch);

if (curl_errno($ch)){

    echo curl_error($ch);
   
}else{

    curl_close($ch);
   
}


?>

 

In the source page it says:

<option value="5d5aedc1ab7ba7e79f8ccbdd9d031131">10 Minuten</option>

Note: This value changes every time

 

I tried something like:

$data = explode("<option value=",$bestand);
foreach ($data as $value) {
echo $value;
}

 

But unfortunately this didn't work :(

 

Any suggestions?

Link to comment
Share on other sites

Isn't there a way to grab the text between

<option value="

and

">10 Minuten</option>

?

 

yep

<?php
$data = '<option value="Hello Boy">10 Minuten</option>';
if (preg_match('%<option value="(.*?)">10 Minuten</option>%', $data, $regs)) {
$result = $regs[1];
} else {
$result = "";
}
echo "found: $result"
?>

Link to comment
Share on other sites

Wow thanks!

... I used it on my cURL source of a loaded page but it doesn't return anything :(

 

My code:

<?php

set_time_limit(0);


$ch = curl_init("X.PHP");
$usecookie = "cookie.txt"; 
       curl_setopt($ch, CURLOPT_COOKIEJAR, $usecookie);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $usecookie);    
$data = curl_exec($ch);

if (curl_errno($ch)){

    echo curl_error($ch);
   
}else{

    curl_close($ch);
   
}



if (preg_match('%<option value="(.*?)">10 Minuten</option>%', $data, $regs)) {
$result = $regs[1];
} else {
$result = "";
}
echo "found: $result"

?>

Link to comment
Share on other sites

Well, if you hadn't set any other cURL options then $data wouldn't have contained the returned string, it would have just been output to the browser. You need to set RETURN_TRANSFER to true if you wish to work with the returned data rather than just outputing it straight away.

 

curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);

 

There's no reason why you should have to write it to a text file. I can only assume that in doing that you added the above option?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.