簡體   English   中英

如果/其他通過代理進行https連接

[英]If/else for https connection through proxy

在執行其他任何操作之前,我正在使用此代碼檢查代理服務器。 連接正常時,我有一條file_get_contents線可以連接到網站。

$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嗎? 是否有可能有一個if / else語句,該語句允許我在沒有警告的情況下執行某些操作,並在有警告的情況下停止執行操作? 我已經看了很多,並且在谷歌上搜索了很多,但是沒有找到我可以嘗試的任何東西。

謝謝你的幫助!

嘗試使用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

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM