fsockopen() expects parameter 2 to be long, string. [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomfra
Forum Contributor
Posts: 126
Joined: Wed Jun 23, 2004 12:56 pm
Location: Prague, Czech Republic

fsockopen() expects parameter 2 to be long, string. [SOLVED]

Post by tomfra »

Here is my problem:

I need to connect to a server via proxy server(s) using fsockopen. There is a list of proxies in a file called proxies.csv in the standard "IP:Port" format.

I pick a random one using this code:

Code: Select all

$data_file = fopen('proxies.csv', 'r');

while (!feof ($data_file)) {
  $data_file_array = explode(':', fgets($data_file));
  $ProxyIP[] = $data_file_array[0];
  $ProxyPort[] = $data_file_array[1];
}
fclose ($data_file);
This code works just fine. Then I open the fsockopen connetion with:

Code: Select all

$fp = fsockopen($ProxyIP[0], $ProxyPort[0], $errno, $errstr, 30);
The rest of the fsockopen related code is very standard so no need to post it here.

Now there is a problem with $fp because if I use it as is, I get this error message:

Code: Select all

fsockopen() expects parameter 2 to be long, string given in...
It is apparently having problem with the $ProxyPort[0], part. If I replace it with the port number - e.g.:

Code: Select all

$fp = fsockopen($ProxyIP[0], 80, $errno, $errstr, 30);
...it works. But if use $ProxyPort[0] (which does return the correct port number as it should when I echo it), I get the above error message.

Any idea what this could be caused by? I've tried just about everything and in my opinion, it should work. But it does not for some reason.

Thanks for any help!

Tomas
Last edited by tomfra on Tue Nov 23, 2004 9:32 pm, edited 1 time in total.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

hmm... i would think php would cast it to an int for you if needed. maybe your version of php doesnt do this

try

Code: Select all

$fp = fsockopen($ProxyIP[0], (int) $ProxyPort[0], $errno, $errstr, 30);
tomfra
Forum Contributor
Posts: 126
Joined: Wed Jun 23, 2004 12:56 pm
Location: Prague, Czech Republic

Post by tomfra »

Yes, this did the trick!

Thanks a lot!!!

Tomas
tomfra
Forum Contributor
Posts: 126
Joined: Wed Jun 23, 2004 12:56 pm
Location: Prague, Czech Republic

Post by tomfra »

And I may have one more question, that is related to fsockopen too... If connection to the first proxy on the list (or a random one) fails, I'd like to "force" it to try another one and another one until it succeed. But it looks like if I use "if (!$fp)" to detect if it worked, it will return true only if the connection fails completely, but not if the connection was refused, page not found etc.

What would you recommend as a possible solution? I think I may need to detect the response sent from the remote server but don't know how.

As always, any ideas are welcome!

Tomas
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

i would do something like this

Code: Select all

<?php

while (the are more proxys to try in our list) {
   $fp = fsockopen(blah);
    $meta = stream_get_meta_data($fp);
    if (200 ok response in $meta) {
        $success = true;
        break;
    }

}


// to check the response headers you may not need to use stream_get_meta....
// i know this works on fopen(), but never tried it on fsockopen()

$fp = fopen('url', 'r');
print_r($http_response_header); // this var magically appears


?>
im not sure what response codes would be acceptable for your situation, but you get the point.
Post Reply