Home  >  Article  >  Backend Development  >  PHP's powerful CURL POST class

PHP's powerful CURL POST class

*文
*文Original
2017-12-29 17:57:081805browse

This article mainly introduces the powerful PHP POST submission data class in detail. The code is concise and has certain reference value. Interested friends can refer to it. I hope to be helpful.

The specific content is as follows

<?php 
class Request{
  public static function post($url, $post_data = &#39;&#39;, $timeout = 5){//curl

    $ch = curl_init();
 curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_POST, 1);
    if($post_data != &#39;&#39;){


      curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

    }
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_HEADER, false);
 $file_contents = curl_exec($ch);
    curl_close($ch);
    return $file_contents;

  }

  public static function post2($url, $data){//file_get_content
    $postdata = http_build_query(
      $data
    );
    $opts = array(&#39;http&#39; =>
           array(
             &#39;method&#39; => &#39;POST&#39;,
             &#39;header&#39; => &#39;Content-type: application/x-www-form-urlencoded&#39;,
             &#39;content&#39; => $postdata
           )

    );

    $context = stream_context_create($opts);
    $result = file_get_contents($url, false, $context);
    return $result;

  }
 public static function post3($host,$path,$query,$others=&#39;&#39;){//fsocket

    $post="POST $path HTTP/1.1\r\nHost: $host\r\n";
    $post.="Content-type: application/x-www-form-";
    $post.="urlencoded\r\n${others}";
    $post.="User-Agent: Mozilla 4.0\r\nContent-length: ";
    $post.=strlen($query)."\r\nConnection: close\r\n\r\n$query";
    $h=fsockopen($host,80);
    fwrite($h,$post);
    for($a=0,$r=&#39;&#39;;!$a;){
        $b=fread($h,8192); 
        $r.=$b;
        $a=(($b==&#39;&#39;)?1:0); 

      }
    fclose($h);
    return $r;

  }

}

?>

Related recommendations:

php uses snoopy and curl to simulate login examples to share

PHP curl disguise source information

##PHP learning CURL crawler example

The above is the detailed content of PHP's powerful CURL POST class. For more information, please follow other related articles on the PHP Chinese website!

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