PHP使用代理IP的通用代码示例

创建于:2021/2/27 7:58:49 阅读: 0

<?php

/**

 * 流量订单demo

 */

class FlowOrderDemo

{

    public $orderId = "";    #用户编号

    public $secret = "";     #秘钥


    public $proxyHost='flow.hailiangip.com';

    public $proxyPort=14223;

    public $user='proxy';

    public $proxyServer;


    public function __construct(){

        $password=$this->createPwd();

        $this->proxyServer='http://'.$this->user.':' . $password . '@' . $this->proxyHost . ':' . $this->proxyPort;

    }


    public function createPwd(){

        $timestamp=time();      #当前时间戳

        $sign = strtolower(md5('orderId='.$this->orderId.'&secret='.$this->secret.'&time='.$timestamp));


        $password = 'orderId='.$this->orderId.'&time='.$timestamp.'&sign='.$sign."&pid=-1&cid=-1&uid=&sip=0&nd=0";


        return $password;

    }


    public function httpProxyWithPassRequest($targetUrl){

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $targetUrl);

        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, false);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        // 设置代理服务器

        curl_setopt($ch, CURLOPT_PROXYTYPE, 0); //http

        curl_setopt($ch, CURLOPT_PROXY, $this->proxyServer);

        // 设置验证信息

        curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);

        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;)");

        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);

        curl_setopt($ch, CURLOPT_TIMEOUT, 5);

        curl_setopt($ch, CURLOPT_HEADER, true);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $result = curl_exec($ch);

        curl_close($ch);

        return $result;

    }


}

$targetUrl = "http://api.hailiangip.com:8422/api/myIp";   #要访问的目标页面

$flow=new FlowOrderDemo();

$data=$flow->httpProxyWithPassRequest($targetUrl);

var_dump($data);


浏览 |

评论 0 |

更多
表情