fopensock not working.

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
freakofnature
Forum Newbie
Posts: 4
Joined: Thu Mar 18, 2004 10:03 am
Location: Syracuse NY

fopensock not working.

Post by freakofnature »

I have code that I developed a while ago, and inserted it into a site I am working on now. My development host is Red Hat Linux, Apache 1.3 server, MySQL server. This code works perfectly on it. My client has installed Apache 1.3 for Windows on his system and MySQL, and the site is up and running. However, this one method in the code does not work. Are there any options that need to be enabled for this to work? Is there something here that is not suppported in the Windows version of PHP? The symptoms are that it just plain freezes, like the fopensock is waiting to open the socket but never does.

Code: Select all

function loadImage($url) {
	
  if ( !preg_match('/^(http:\/\/)?(ї\w\-\.]+)\:?(ї0-9]*)\/(.*)$/', $url, $url_ary) ) {
  print_r(array_values($url_ary));
  	echo "Error - invalid image url - $filename";
  }
 
  $base_get = '/' . $url_aryї4];
  $port = ( !empty($url_aryї3]) ) ? $url_aryї3] : 80;
  if ( !($fsock = @fsockopen($url_aryї2], $port, $errno, $errstr)) )
    {
      echo "<br>Error<br>$errno<br>$errstr\n";
      return false;
    &#125;
  
  @fputs($fsock, "GET $base_get HTTP/1.1\r\n");
  @fputs($fsock, "HOST: " . $url_ary&#1111;2] . "\r\n");
  @fputs($fsock, "Connection: close\r\n\r\n");
  
  unset($data);
  while( !@feof($fsock) )
    &#123;
      $data .= @fread($fsock,1024);
    &#125;
  @fclose($fsock);

  if (!preg_match('#Content-Length\: (&#1111;0-9]+)&#1111;^ /]&#1111;\s]+#i', $data, $file_data1) || !preg_match('#Content-Type\: image/&#1111;x\-]*(&#1111;a-z]+)&#1111;\s]+#i', $data, $file_data2))
    &#123;
      echo "Error getting image<br>\n";
      return false;
    &#125;
  $filesize = $file_data1&#1111;1];
  $filetype = $file_data2&#1111;1];
  
  $data = substr($data, strlen($data) - $filesize, $filesize);

  return $data;
&#125;
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Re: fopensock not working.

Post by TheBentinel.com »

The fsockopen man page doesn't say that it does or doesn't work with Windows, but one of the user notes suggests that it does.

If you pull all the @'s off of your calls, do you get any error messages?
freakofnature
Forum Newbie
Posts: 4
Joined: Thu Mar 18, 2004 10:03 am
Location: Syracuse NY

Post by freakofnature »

Getting an error saying that it is timing out. The customer has the server set up internally on a network...could a proxy server be what's causing this? Is there a way to set thhis up to use the proxy if this is the case?
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Post by TheBentinel.com »

freakofnature wrote:Getting an error saying that it is timing out. The customer has the server set up internally on a network...could a proxy server be what's causing this? Is there a way to set thhis up to use the proxy if this is the case?
If they have a proxy server, you may need to point at that port. Is it 8080? It's worth a whack, since it's so easy to try.

Can you get to the resource using file_get_contents()? At least you'd narrow down the fsockopen() function versus general connectivity issues.
freakofnature
Forum Newbie
Posts: 4
Joined: Thu Mar 18, 2004 10:03 am
Location: Syracuse NY

Post by freakofnature »

Changing the host and port to the proxy got it to not timeout...but it is not getting the requested data either. I'm sending the same HTTP headers to the proxy that I send to the URL I'm connecting to. I could not disable the proxy settings either to get around it (incase it was only there for monitoring and not internet sharing). I saw some random tidbits about proxies on php.net, but nothing conclusive. I did notice my server is running mod_proxy, I will look into that and see what that is...if I can even use it. Any other ideas?
Post Reply