jconnop Posted May 14, 2007 Report Share Posted May 14, 2007 Hi there,I have been for the last few hours trying to get a php script to connect to the uTorrent webUI to grab the info from the /gui/?list=1 page and parse it.I have managed to get past the logging in issues by sending Authorization: Basic username:password in the header, but after that, any attempt to fread, fgets, file_get_contents, whatever will leave the script sitting there doing nothing until it times out.Has anyone managed to get a PHP script to talk nicely to the webUI? If so could you either explain how you did it or post some code or something. It would be greatly appreciated Thanks,Jesse Link to comment Share on other sites More sharing options...
patrickbrans Posted May 14, 2007 Report Share Posted May 14, 2007 <?$webUI_url = "URL";$webUI_port = PORT;$webUI_user = "USERNAME";$webUI_pass = "PASSWORD";$webUI_cont = '';$webUI_fp = fsockopen($webUI_url, $webUI_port);if (!$webUI_fp){ return false;}fputs($webUI_fp, "GET /gui/?list=1 HTTP/1.0\r\nHost: $webUI_url\r\n");fputs($webUI_fp, "Authorization: Basic " . base64_encode ("$webUI_user:$webUI_pass") . "\r\n\r\n");do { $webUI_cont_new = fread($webUI_fp,4096); $webUI_cont .= $webUI_cont_new; if(strlen($webUI_cont_new) != 4096) { break; }}while(true);fclose($webUI_fp);$webUI_cont = substr($webUI_cont, strpos($webUI_cont,"\r\n\r\n")+4);echo $webUI_cont;?>Hope this works for you Link to comment Share on other sites More sharing options...
jconnop Posted May 15, 2007 Author Report Share Posted May 15, 2007 Thanks for that, it does work, albeit slowly.It hangs on 'Waiting for [host]' for about a minute before displaying the result. I'm guessing the scripts I wrote myself were doing the same thing, except I was trying with a huge length param for fread so I'm assuming that's where it is hanging.I put a timer around the code and it is taking times like: 60.04s, 60.18s, 60.06s, etc. I thought this might be to do with the fsockopen as I believe the default timeout is 60s? Which would explain the times of slightly over 60s. 60s to time out and a little more to execute the rest of the script. However when I forced the timeout of fsockopen to be something like 10, it had no effect.Therefore I have to assume it's the fread loop that is taking all the time, does it also do this for you or is it instant?I don't mind having the script take this long really, just an annoyance and I thank you very much for the code you have posted. Link to comment Share on other sites More sharing options...
Lord Alderaan Posted May 15, 2007 Report Share Posted May 15, 2007 cURL might be easier and possibly faster but requires enabling that extension in php (unless its already enabled of course).<?php$webUI_url = "URL";$webUI_port = "PORT";$webUI_user = "USERNAME";$webUI_pass = "PASSWORD";$ch = curl_init();curl_setopt($ch, CURLOPT_URL, "http://$webUI_url:$webUI_port/gui/?list=1" );curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);curl_setopt($ch, CURLOPT_USERPWD, "$webUI_user:$webUI_pass");if ( $response = curl_exec( $ch ) ) { echo $response;}else { echo 'Server not found';}curl_close($ch);?> Link to comment Share on other sites More sharing options...
jconnop Posted May 15, 2007 Author Report Share Posted May 15, 2007 The curl method works well thank you. Though I did get the first code working fast too.It would seem that it had something to do with the loop you have there, maybe the strlen test I'm not sure, but with the code as I've posted below it processes in about 0.1secs - much better <?php$webUI_url = "";$webUI_port = ;$webUI_user = "";$webUI_pass = "";$webUI_cont = '';$webUI_fp = fsockopen($webUI_url, $webUI_port, $errno, $errstr, 10);if (!$webUI_fp){ return false;}fputs($webUI_fp, "GET /gui/?list=1 HTTP/1.0\r\nHost: $webUI_url\r\n");fputs($webUI_fp, "Authorization: Basic " . base64_encode ("$webUI_user:$webUI_pass") . "\r\n\r\n");$webUI_cont_new = fread($webUI_fp,8192);$webUI_cont .= $webUI_cont_new;fclose($webUI_fp);$webUI_cont = substr($webUI_cont, strpos($webUI_cont,"\r\n\r\n")+4);echo $webUI_cont;echo number_format(bcsub($endtime,$starttime,6),3);echo " s ";?>Although with many reads in quick succession it seems the response gets cut off sometimes, I was thinking of just doing a test to see if the final } was in the string, but it seems the curl method works fine every time so I'll just go with that. Thank you both for the help. Link to comment Share on other sites More sharing options...
Lord Alderaan Posted May 16, 2007 Report Share Posted May 16, 2007 The reason it gets cut off is because you only read the buffer once and you only read 8kb of it. If there is more you won't get it. Thats why there was a loop the code from patrickbrans. It keeps checking for data until it gets it all. Making your own socket connections requires a bit of knowledge of how it works.cURL takes care of the (proper) http connection between your script and the webpage you try to access and is considered easier and might be faster. Making your own socket connection allows for more control but might also requires knowledge, often needs debugging and isn't supported on a lot of php web servers (not that cURL is supported everywhere...).If you wanna learn about sockets this is actually a good test case to fool around with to get it working properly. But if you just want to get your php interface working correctly without any more hassle I suggest the cURL method. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.