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.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程