Jump to content

cURL getting specific info from page. Simple. pls help!


daydreamer

Recommended Posts

I want to be able to retrieve how many texts I have left so I can use it as a variable in my code.

 

On the 02 website, it says "You have X texts remaining".

 

How can I retrieve number X?

 

The html code on the 02 site containing this number:

 

<div style="width: 70%; float: left;">
<div style="float: left;">
</div>
<div style="padding: 3px 0pt 0pt 5px; float: left;">
<label title="Use free text. 6 remaining" for="compose.paymentType" style=""> Use free text. You have 6 texts remaining </label>
</div>
</div>
</div>

 

I have already used cURL at this point in my script, so this page will be loaded on my server cURL session. Just need advice on how I can use PHP code to retrieve this number.

 

Any help appreciated - Thanks!  ;D

 

 

 

Find it with a regular expression.

 

E.G

 

<?php
$string = <<<EOF

<div style="width: 70%; float: left;">
<div style="float: left;">
</div>
<div style="padding: 3px 0pt 0pt 5px; float: left;">
<label title="Use free text. 6 remaining" for="compose.paymentType" style=""> Use free text. You have 6 texts remaining </label>
</div>
</div>
</div>

EOF;


preg_match("/(?<=You have )[0-9]+/",$string,$matches);
print_r($matches);
?>

 

I am using Curl and PHP to log into a SMS mobile phone texting site (02.co.uk), which gives you 10 free texts a month.

 

On the compose text page, it will display how many free texts you have remaining for this month. I want to be able to get this number and store it in a variable in my code. My first post shows where exactly in HTML code on the 02 site the number is. Let me know if you need more details.

 

This is how im using curl (excluding cookies and other connection settings):

 

curl_setopt($curl, CURLOPT_URL, "http://sendtxt.o2.co.uk/sendtxt/action/compose"); //go to compose page 
$xxx = curl_exec($curl); // execute above, save page in $xxx
curl_close ($curl);   // close curl session
echo $xxx;     // output compose page

 

So the page is stored in $xxx. I need to get the number from this page.

 

Hey KrisNz, I tried your example, but this is what I got when run:

 Use free text. You have 6 texts remaining
EOF; preg_match("/(?<=You have )[0-9]+/",$string,$matches); print_r($matches); ?>

 

What are the EOF tags you use? "End Of File"?

 

But thanks for the example, I am going to look up more about the preg_match function!

 

 

 

Oops.

 

KrisNz your code returns:

Array ( [0] => 6 )

 

just what I needed thanks! I was not going through my apache server to read the file.

 

Ok. So this is what I am using:

 

preg_match("/(?<=You have )[0-9]+/",$xxx,$matches);
echo "Number of free texts left: ";
echo $matches[0];  // output array position 0 - number of free texts

 

It works fine 8). But...

 

What do all the slashes and symbols mean in this part of the code:

 

("/(?<=You have )[0-9]+/",

 

And also, the text messages will start off from 10. So will the [0-9] part work? I tried to change the 9 to a 10, but I get 0 outputted, can you explain this to me?

 

thanks!

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.