详解PHP fsockopen的使用方法(获取页面的头信息)

本文详细介绍了PHP中的fsockopen函数,该函数用于建立到指定目标的套接字连接,并通过示例展示了如何使用此函数进行HTTP请求。文章还提到了在PHP.ini配置文件中允许URL fopen选项的重要性。

  • 1.PHP fsockopen函数说明:

    Open Internet or Unix domain socket connection(打开套接字链接)

    Initiates a socket connection to the resource specified by target .

    fsockopen() returns a file pointer which may be used together with the other file functions (such as fgets() , fgetss() , fwrite() , fclose() , and feof() ).就是返回一个文件句柄

    开启PHP fsockopen这个函数

    PHP fsockopen需要 PHP.ini 中 allow_url_fopen 选项开启。

    1. $fp = fsockopen("www.example.com",
       80, $errno, $errstr, 30);   
    2. if (!$fp) {   
    3. echo "$errstr ($errno)<br />/n";   
    4. } else {   
    5. $out = "GET / HTTP/1.1/r/n";   
    6. $out ."Host: www.example.com/r/n";   
    7. $out ."Connection: Close/r/n/r/n";   
    8.  
    9. fwrite($fp, $out);   
    10. while (!feof($fp)) {   
    11. echo fgets($fp, 128);   
    12. }   
    13. fclose($fp);   
    14. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值