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๐Ÿ˜›ort" format.

I pick a random one using this code:

$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:

$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:

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.:

$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

    Try trimming the line before you explode it: it still has the end-of-line character attached, and that ends up on the end of $ProxyPort[0]. If that's not enough, just adding 0 would do the conversion. Or cast it to an integer explicitly.

      Thanks! using (int) $ProxyPort[0] solved the problem.

      Tomas

        Write a Reply...