如果/ else用于https连接通过代理

时间:2017-05-24 08:49:18

标签: php if-statement proxy

在执行任何其他操作之前,我正在使用此代码检查代理服务器。当连接正常时,我有$host = '123.45.678.90'; $port = 80; $waitTimeoutInSeconds = 1; if($fp = fsockopen($host,$port,$errCode,$errStr,$waitTimeoutInSeconds)){ $aContext = array( 'http' => array( 'proxy' => 'tcp://123.45.678.90:80', 'request_fulluri' => true, ), ); $cxContext = stream_context_create($aContext); // connect to website using a proxy server $file_content = file_get_contents('https://www.anything.com', False, $cxContext); } else { // It didn't work } fclose($fp); 行连接到网站。

Cannot connect to HTTPS server through proxy

但是虽然代理连接成功,但我看到了这一点:警告 Vehicle#loadingDate?有没有机会有一个if / else语句,如果没有警告就允许我做某事,如果有警告就停止做某事?我已经看过很多了,但是找不到任何可以尝试的东西。

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

尝试使用curl代替file_get_contents,它会起作用

<?php

$url = 'https://www.google.com';
// to check your proxy
// $url = 'http://whatismyipaddress.com/';
$proxy = '50.115.194.97:8080';

// create curl resource
$ch = curl_init();

// set options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // read more about HTTPS     http://stackoverflow.com/questions/31162706/how-to-scrape-a-ssl-or-https-    url/31164409#31164409
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1;     en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');

// $output contains the output string
$output = curl_exec($ch);

// close curl resource to free up system resources
curl_close($ch); 

echo $output;

?>

来自PHPhil

答案 1 :(得分:0)

if($fp = fsockopen($host,$port,$errCode,$errStr,$waitTimeoutInSeconds)没有意义,如果此代理有效,您只需使用:

$aContext = array(
    'http' => array(
        'proxy' => 'tcp://123.45.678.90:80',
        'request_fulluri' => true,
    ),
);
$cxContext = stream_context_create($aContext);
$file_content = file_get_contents('https://google.com', False, $cxContext);
print $file_content;
相关问题