search
Homephp教程PHP源码PHP 以 Post 方式请求网页数据

PHP 以 Post 方式请求网页数据

/**
   * Post 方式请求网页数据
   *
   * @param string $url     网页地址
   * @prarm string $host    主机
   * @param string $session 会话值
   * @prarm string $type    类型(POST、GET)
   * @prarm string $port    端口
   * @prarm string $data    数据
   */
  function getPageConent( $url, $host,  $data = "" ,$session = "", $type = "POST", $port = "") {
      
      if( empty($port) ) $port = 80;     
      
      /* 请求数据 */
      $post_data = $data;
     $lenght = strlen($post_data);
     
     $headers  = "{$type} {$url} HTTP/1.1\r\n";
     $headers .= "Accept: * /*\r\n";
     $headers .= "Content-Type: application/x-www-form-urlencoded\r\n";
     $headers .= "User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB6; CIBA; .NET CLR 4.0.20506)\r\n";
     if($session != "" ) $headers .= "Cookie:JSESSIONID={$session}\r\n";
     $headers .= "Host: {$host}:{$port}\r\n";
     $headers .= "Content-Length: {$lenght}\r\n";
     $headers .= "Connection: Close\r\n\r\n";
     $headers .= $post_data;
 
     if( $fp = fsockopen( $host, $port, $errno, $errstr, 100) ) {
         fwrite($fp, $headers);        
           $header   = fread($fp, 1024);    
           $content  = fread($fp, 1024);    
           $content .= fread($fp, 1024);
           $content .= fread($fp, 1024);
           $content .= fread($fp, 1024);
         fclose($fp);        
     }    
     if( $data != "" ) {
         echo $headers;
         echo "";
         echo $header;
         echo "";
         echo $content;
         echo "";
         exit;
     } else {
         return $content;
     }    
  }
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft