file_get_contents ผ่าน tor

ดังนั้นฉันจึงมองหาชื่อเพจที่ใช้ php หลังจากค้นคว้ามาเป็นเวลา 5 วินาที ฉันพบคำตอบที่นี่:

        function get_title($url){
        $str = file_get_contents($url);
        if(strlen($str)>0){
          $str = trim(preg_replace('/\s+/', ' ', $str)); 
          preg_match("/\<title\>(.*)\<\/title\>/i",$str,$title); 
          return $title[1];
        }
      }

แต่ฉันต้องการสิ่งนั้นผ่าน Tor Proxy ดังนั้นฉันใช้เวลา 5 วินาทีในการค้นคว้าเกี่ยวกับไซต์นี้และภูมิปัญญาของคุณ ฉันพบว่า:

        $aContext = array(
        'http' => array(
            'proxy' => 'proxy:port',
            'request_fulluri' => true,
        )
    );

    $cxContext = stream_context_create($aContext);

เมื่อรวมทุกอย่างเข้าด้วยกันฉันทำสิ่งนี้:

        $aContext = array(
        'http' => array(
            'proxy' => '127.0.0.1:9150',
            'request_fulluri' => true,
        )
    );

    $cxContext = stream_context_create($aContext);

    function get_title($url){
        global $cxContext;
        $str = file_get_contents($url, False, $cxContext);

        if(strlen($str)>0){
          $str = trim(preg_replace('/\s+/', ' ', $str));
          preg_match("/\<title\>(.*)\<\/title\>/i",$str,$title); 
          return $title[1];
        }
      }

echo get_title('http://' . $theonionurl);

แต่นั่นไม่ได้ผล บันทึกแสดง:

คำเตือน PHP: file_get_contents (http://the_onion_address_to_check.onion): ไม่สามารถเปิดสตรีม: การเชื่อมต่อถูกปฏิเสธใน /var/www /html/mychecker.php ออนไลน์ที่ 44 ผู้อ้างอิง: http://my_onion_address.onion/mychecker.php

ฉันเปลี่ยนพอร์ตเป็น 9050 แต่ก็ยังใช้งานไม่ได้

ผมทำอะไรผิดหรือเปล่า ???

(เห็นได้ชัดว่าฉันตรวจสอบแล้ว URL ที่จะตรวจสอบนั้นถูกต้องและเข้าถึงได้ผ่านเบราว์เซอร์ของ Tor)


person Guy Dresher    schedule 15.06.2018    source แหล่งที่มา


คำตอบ (2)


Tor ติดตั้งและทำงานอยู่บนระบบของคุณหรือไม่? การเชื่อมต่อถูกปฏิเสธจะระบุว่าไม่มีสิ่งใดกำลังฟังบนพอร์ตนั้น

ก่อนอื่นคุณต้องติดตั้งและเรียกใช้ Tor ก่อนจึงจะสามารถใช้เพื่อเชื่อมต่อกับไซต์ได้

นอกจากนี้ พอร์ต 9050 ยังเป็นพร็อกซี SOCKS ไม่ใช่พร็อกซี HTTP ดังนั้น คุณจะไม่สามารถใช้กับตัวเลือกบริบทพร็อกซีสตรีม HTTP ได้ เนื่องจากใช้งานได้กับพร็อกซี HTTP เท่านั้น

คุณควรใช้ curl แทนตัวเลือกพร็อกซีหากคุณต้องการใช้ Tor:

$ch = curl_init('http://example.onion/');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_PROXYTYPE      => CURLPROXY_SOCKS5_HOSTNAME,
    CURLOPT_PROXY          => '127.0.0.1:9050',
    CURLOPT_HEADER         => 0,
    CURLOPT_FOLLOWLOCATION => 1,
    CURLOPT_ENCODING       => '',
    CURLOPT_COOKIEFILE     => '',
]);

$response = curl_exec($ch);

if ($response === false) {
    echo sprintf(
        "Request failed.  Error (%d) - %s\n",
        curl_errno($ch),
        curl_error($ch)
    );
    exit;
}

if (preg_match('/<title>(.*)<\/title>', $response, $match)) {
    echo "The title is '{$match[1]}'";
} else {
    echo "Did not find title in page."
}
person drew010    schedule 17.06.2018
comment
ขอบคุณ! ขดกำลังทำงาน! [ต้องติดตั้งก่อน] - person Guy Dresher; 17.06.2018
comment
@Borna พอร์ตเริ่มต้นยังคงเป็น 9050 Tor Browser Bundle ใช้ 9150 - person drew010; 27.09.2018

$aContext ของคุณอยู่นอกฟังก์ชัน
ย้ายเข้าไปภายในฟังก์ชันและควรจะใช้งานได้

function get_title($url){
    $aContext = array(
    'http' => array(
        'proxy' => '127.0.0.1:9150',
        'request_fulluri' => true,
    )
    );

    $cxContext = stream_context_create($aContext);

    $str = file_get_contents($url, False, $cxContext);

    if(strlen($str)>0){

      $str = trim(preg_replace('/\s+/', ' ', $str));
      preg_match("/\<title\>(.*)\<\/title\>/i",$str,$title); 
      return $title[1];
    }
  }

echo get_title('http://' . $theonionurl);

ไม่แน่ใจเกี่ยวกับสิ่งสากลนั้น
ฉันไม่เคยใช้มันและพบว่าปลอดภัยกว่าด้วยตัวแปรท้องถิ่น

person Andreas    schedule 15.06.2018
comment
ไม่! คำเตือน PHP: file_get_contents (the_onion_address_to_check.onion): ไม่สามารถเปิดสตรีม: การเชื่อมต่อถูกปฏิเสธใน /var/www/html/mychecker .php ออนไลน์ 109 ผู้อ้างอิง: my_onion_address.onion/mychecker.php ยังคงอยู่ - person Guy Dresher; 16.06.2018