duanlanzhi5509 2011-02-21 14:35
浏览 285

PHP Socket通过代理连接

I'm building a webmail-client-like application, and I'm having issues connecting to POP3 through our beloved corp proxy.

Due to client requirements, I'm obligated to use sockets to talk with their POP3 server (they actively specified this is mandatory and they also explained they have a proxy between the app server and the POP) so besides enabling me to work this is kind of the only option I have.

Does anyone know how to do this?

UPDATED TO ADD:

Here's my socket handler:

<?php
class Socket {
  protected $handle = null;
  protected static $status = null;
  public function __construct($server, $port){
    if(!$this->connect($server, $port)){
      throw new UnexpectedValueException("Server $server in port $port unreachable.");
    }
  }
  public static function getStatus(){
    return self::$status;
  }  
  public function write($string){
    fputs($this->handle, $string);
    return $this;
  }
  public function read($lines = 1){
    $lines = abs((int)$lines);//people can be morons
    for($i = 0; $i < $lines; $i++){
      $response []= $this->getLine();
    }
    if($lines==1){
      $response = $response[0];
    }
    return $response;
  }
  public function connect($server, $port){
    $errNo = 0;
    $handle = fsockopen($server, $port);
    if(!$handle){
      return false;
    }
    $this->handle = $handle;
    return $this;
  }
  public function disconnect(){
    if(gettype($this->handle)=='resource'){
      fclose($this->handle);
    }
  }
  public function __destruct(){
    $this->disconnect();
  }
  protected function getLine(){
    return fgets($this->handle);
  }
}

I'm using it like so:

$plug = new Socket('mx.mailserver.com',110);
$plug->write("USER $username
")->read();
$could = $plug->write("PASS $password
")->read();
if(strpos($could,'+OK')!==false){
  $mails = $plug->write("STAT
");
}

And right now our proxy is not letting me out; situation I would like to know how to revert.

  • 写回答

2条回答 默认 最新

  • drtohng5613 2011-03-16 11:42
    关注

    First, have you tested with telnet - i.e. if you are on the same server / workstation that the PHP script is running on, and you issue:

    telnet mx.mailserver.com 110
    

    Do you get the greeting? (you're also not reading that - so your read from the command where you send the username is going to get that as a response, so your code will work but largely by accident.

    If you can't get it to work via telnet then stop and get that working first - no point proceeding otherwise.

    You may need to send at the end of each command.

    $plug->write("USER $username
    ")->read();
    

    So you need to do something like:

    $plug = new Socket('mx.mailserver.com',110);
    $greeting = $plug->read(); // check this said +OK
    $plug->write("USER $username
    ")->read(); // check this said +OK
    $could = $plug->write("PASS $password
    ")->read();
    if(strpos($could,'+OK')!==false) {
        $mails = $plug->write("STAT
    ");
    }
    

    I'm not entirely sure about your strpos() line - but I can't test that right now.

    As I say, before you do ANYTHING else verify that it all works ok on Telnet.

    If it does, then monitor the server logs (if possible) and see whether you're getting in touch with the POP3 server and what it's getting.

    Here's an example sequence:

    telnet pop.blah.com 110

    +OK Welcome to MailEnable POP3 Server
    

    USER matthew

    +OK
    

    PASS mypassword

    +OK 
    

    STAT

    +OK 0 0
    

    Hope this helps.

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料