PHP POST Request HTTP interface

Source: Internet
Author: User

Here is the curl way, so we must first ensure that the Curl function library open: In the php.ini file, extension=php_curl.dll before the semicolon removed

Code on:

/**
 * analog post for URL request
 * @param string $url
 * @param array $post _data
/function request_post ($url = ' ', $post _data = Array ()) {//url is required  if the address does not require an argument, the If
     (empty ($url)) {return
         false;
     }
     
    if (!empty ($post _data)) {
     $params = ';
      foreach ($post _data as $k => $v) 
      { 
          $params. = "$k =". UrlEncode ($v). "&";
         $params. = "$k =". $v. "&";
      }
      $params = substr ($params, 0,-1); 
     $ch = Curl_init ()///Initialize Curl
     curl_setopt ($ch, Curlopt_url, $url);//crawl The specified page
     curl_setopt ($ch, Curlopt_header, 0)//Set Header
     curl_setopt ($ch, Curlopt_returntransfer, 1),//request result string and output to screen
     curl_setopt ($ch, Curlopt_ POST, 1);//post Submission Mode
     if (!empty ($post _data)) curl_setopt ($ch, Curlopt_postfields, $params);
     $data = curl_exec ($ch);//Run Curl
     curl_close ($ch);
     return $data;
}
Test without parameters
Request_post (' http://www.baidu.com ');
With parameters
$post _data[' id ']= ' 1 ';
Request_post (' http://www.baidu.com ', $post _data);

</pre><pre>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.