[Tfug] php/curl question

James Hood ebenblues at yahoo.com
Wed Oct 10 22:37:12 MST 2007


I have an obscure question about using curl via php and thought maybe
someone could point me in the right direction. Basically, I'm writing a php
script that will post to a web form, capture the resulting html and
then do stuff with it. The problem is, the returned html page is very
long and I seem to be running into threading issues. Here's the basic
script:

$c = curl_init($site->post_url);

curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);

$html = curl_exec($c);
curl_close($c);

// Option 1: save output html to a file
// $handle = fopen("out.html", "w");
// fwrite($handle, $html);
// fclose($handle);
// die();

// Option 2: parse output html directly in the script
$dom = new DOMDocument();
$dom->loadHTML($html);

// Manipulate parsed doc...

When
I do option 1, the output file written contains the entire html output
as I would expect. But when I do option 2, the DOM object only contains
the first couple hundred bytes of the html output. From this behavior,
I'm guessing maybe curl_exec spawns an async thread to complete the http
request and then the php script continues to run while curl's running
in the background. This is not the behavior I want. I want some way to
tell the script to wait until curl_exec is finished before processing
the output html...does anyone know how I could do that or can someone point me
in the right direction? I haven't been able to find any help via
google, but I'm also not sure exactly what to search for in this case.

I'm pretty sure it's something to do with curl and not the dom parsing because I made another script read in the file saved under option 1 and the DOM document that was created was complete as I would expect it. Another oddity is that if I do $dom->saveHTML() after the loadHTML call, the full page is output to the the screen (but it does take some time loading it since the page is very large).

Anyway, any help is appreciated.

Thanks,
James 
------------------------------
"The humble learn the fastest because they don't waste time on defending a false image."




       
____________________________________________________________________________________
Moody friends. Drama queens. Your life? Nope! - their life, your story. Play Sims Stories at Yahoo! Games.
http://sims.yahoo.com/  




More information about the tfug mailing list