The Curl function in PHP

Source: Internet
Author: User

1. Installation configuration for Curl:
Installation under Linux:
http://php.net/manual/zh/curl.setup.php
Installation under Windows:
Open:
Extension=php_curl.dll//Note the corresponding version of the DLL file

2. Curl Use steps

<?php//1. Initialize $curl = Curl_init ();//2. Configuration related parameter information curl_setoptcurl_setopt ($curl, constant parameters, optional parameters);//3. Execute $res = curl_exec ($curl);//4. Close link curl_close ($curl);? >

For specific parameter usage information, see: http://php.net/manual/zh/book.curl.php

3. Partial Use cases
3.1 certificate authentication needs to be filtered when using HTTPS

<?php $this->curl->options (Array (Curlopt_ssl_verifypeer = False, Curlopt_ssl_verifyhost = false)); >

3.2 Use the following method when transferring an interface using the body (typically, the authentication method for Java)

/** * Normal function Delivery method (certificate skipped) * Call Base: * $url = "Https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_toke N= $accessToken "; * $res = Json_decode ($this->httpget ($url));    */private function HttpGet ($url) {$curl = Curl_init ();    curl_setopt ($curl, Curlopt_returntransfer, true);    curl_setopt ($curl, Curlopt_timeout, 500);    curl_setopt ($curl, Curlopt_ssl_verifypeer, false);    curl_setopt ($curl, Curlopt_ssl_verifyhost, false);    curl_setopt ($curl, Curlopt_url, $url);    $res = curl_exec ($curl);    Curl_close ($curl);  return $res; }/** * Java Body Authentication Transfer method * Call Base: * $result = List ($return _code, $return _content) = $this->http_post_data ($u   RL, Json_encode (Array ("Key" = $pass)));   * $res = Json_decode ($return _content);   * $return _code = $res->rspmsg;        */Public Function Http_post_data ($url, $data _string) {$ch = Curl_init ();        curl_setopt ($ch, Curlopt_post, 1);        curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_postfields, $data _string); curl_setopt ($ch, Curlopt_httpheader, Array (' Content-type:application/json; Charset=utf-8 ', ' content-length: ' .        strlen ($data _string)));        Ob_start ();        Curl_exec ($ch);        $return _content = ob_get_contents ();        Ob_end_clean ();        $return _code = Curl_getinfo ($ch, Curlinfo_http_code);    Return Array ($return _code, $return _content); }

Debug mode of curl in 3.3 ci frame

$this->curl->debug ();

3.4 CURL Analog file Upload (sound file upload)

$soundurl = ' absolute address of your local sound path such as:/data/sound/ins.amr ';      Initialize      $ch = Curl_init ();      File path address      $furl = "@". $soundurl;      $post _data = Array (          "media" = = $furl      );      Submit file Address      $url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=". $token. " &type=voice ";      Set Variable      curl_setopt ($ch, Curlopt_url, $url);      curl_setopt ($ch, Curlopt_returntransfer, 1);//Whether the execution result is returned, 0 is returned, 1 is not returned      curl_setopt ($ch, Curlopt_postfields, $post _data);      Execute and get results      $output = curl_exec ($ch);      if ($outopt = = = FALSE) {          echo "<br/>", "CUrl Error:". Curl_error ($ch);      }      Curl_close ($ch);      $obj = Json_decode ($output);

3.5 Local download of sound files

$soundfile = $this->curl->simple_get ("http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=". $ token. " &media_id= ". $soundid);      $soundurl = "./sound/". $id. ". Amr ";      File_put_contents ($soundurl, $soundfile);









The Curl function in PHP

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.