Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

PHP Socket Context Options

บทนำ

การเข้าถึง filesystem และ stream wrappers ต่างๆ สามารถปรับแต่งได้ด้วย context options และ parameters ที่กำหนดค่าด้วยฟังก์ชัน stream_context_create() และ stream_context_set_option()

รายการต่อไปนี้แสดง socket context options ที่พร้อมใช้งานสำหรับ wrappers ทั้งหมดที่ทำงานผ่าน sockets เช่น tcp, http และ ftp

bindtoระบุ IP address (IPv4 หรือ IPv6) และ/หรือ port number ที่ใช้เข้าถึง network (ip:port สำหรับ IPv4 [ip]:port สำหรับ IPv6)
backlogจำกัดจำนวน outstanding connections ใน listen queue ของ socket
ipv6_v6onlyOverride ค่า default ของ OS เกี่ยวกับการ map IPv4 เข้า IPv6
so_reuseportอนุญาต multiple bindings ไปยัง ip:port pair เดียวกัน
so_broadcastเปิดใช้งานการส่งและรับข้อมูลไป/มาที่ broadcast addresses
tcp_nodelayหาก TRUE ตั้งค่า SOL_TCP,NO_DELAY=1 อย่างเหมาะสม ปิด TCP Nagle algorithm

ตัวอย่าง

<?php // connect to the internet using the '192.168.0.100' IP $opts = array( 'socket' => array( 'bindto' => '192.168.0.100:0', ), ); // ...and use it to fetch the data echo file_get_contents('https://www.example.com', false, $context); ?>