PHP Curl function to get information about remote host

Source: Internet
Author: User

In the process of developing PHP programmers, it is often necessary to invoke other interfaces. PHP provides us with a range of functions. Curl series functions. The following is a description of the use of these column functions for your own reference and others.

demo.php

<?phpfunction curl_get ($url, $headerArr = ', $cookie = ') {$ch  = curl_init ($url); curl_setopt ($ch,  curlopt_returntransfer, true) curl_setopt ($ch,  curlopt_header, false); curl_setopt ($ch,  curlopt_connecttimeout, 3) curl_setopt ($ch,  curlopt_timeout, 10)//10 seconds Timeout curl_setopt ($ch,  curlopt_ssl_verifypeer, false); curl_setopt ($ch,  curlopt_ssl_verifyhost,  false); if ($ Headerarr) {      curl_setopt ($ch, curlopt_httpheader ,  $HEADERARR );                 }if ($cookie) {curl_setopt ($ch, curlopt_cookie,  $cookie);} $data  = curl_exec ($ch); Curl_close ($ch);return  $data;}   Invoke instance $url =  "Http://192.168.10.26:8801/2/sub-system/user"; $cookie  =  "token= EyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjIyM30.Y25kn6rLQCttGrsEuf4taTqBvKD9oMaZVOEb2L0Ig7U "; $results =curl_get ($url, ", $cookie); $results =json_decode ($results, true); if ($results [' Code ']==0) {return  $results [' Data '];} return  ""; Function curl_post ($url, $postData, $HEADERARR = ", $cookie =") {if (Is_array ($postData)) {$ Postdata=http_build_query ($postData);   //after generating  URL-encode  request string} $ch  = curl_init ( $url); curl_setopt ($ch,  curlopt_returntransfer, true); curl_setopt ($ch, curlopt_header,  FALSE); curl_setopt ($ch,  curlopt_connecttimeout, 3); curl_setopt ($ch,  curlopt_timeout, 10); curl_setopt ($ch,  curlopt_ssl_verifypeer, false); curl_setopt ($ch, curlopt_ssl_verifyhost,   false); curl_setopt ($ch,  curlopt_post, 1)//10 seconds Timeout curl_setopt ($ch,  curlopt_postfields,$ PostData); if ($HEADERARR) {      curl_setopt ($ch,  curlopt_httpheader ,   $HEADERARR);                 }if ($cookie) {curl_setopt ($ch, curlopt_cookie,  $cookie);} $data  = curl_exec ($ch); Curl_close ($ch);return  $data;}   Invoke instance $url =  "http://devwallet.100msh.com/walletadmin/api/wallet/"; $data  = array (' Time ' =>date (' y-m-d h:i:s ', strtotime (' -1 day ')), $des  = new des (' @Wt ^2) v# ');     // php,java, such as general-purpose DES encryption and decryption algorithm class $data =  $des->encrypt (Json_encode ($data)); Headerarr=array (' content-type:text/html ',); $results  = curl_post ($url, $data, $HEADERARR); $results  = json_decode ($results, true); if ($results [' Code ']==0) {return  $results [' Data '];} return  "";? >

Des.class.php

<?php/** * Class Des *  @desc  php,java Universal Des Encryption and decryption algorithm class  *///header (" Content-type: text/html; charset=utf-8 "); class des {    private $ Key;   function __construct ($key)  {       $this->key  =  $key;    }   function encrypt ($input)  {        $size  = mcrypt_get_block_size (' des ',  ' ECB ');     // This function is used to obtain the encoding of the chunk size        $input  =  $this->pkcs5_pad ($input,  $size) ;       $key  =  $this->key;       $td  = mcrypt_module_open (' des ',  ',  ' ECB ',  ');       $iv  =  @mcrypt_create_iv   (mcrypt_enc_get_iv_size ($TD),  mcrypt_rand);        @mcrypt_generic_init ($td,  $key,  $iv);       $data  = mcrypt_generic ($td,  $input) ;       mcrypt_generic_deinit ($TD);       mcrypt_ Module_close ($TD);       $data  = base64_encode ($data);       return  $data;    }   function decrypt ($ encrypted)  {       $encrypted  = base64_decode ($encrypted);        $key  = $this->key;       $td  =  Mcrypt_module_open (' des ', ' ', ' ECB ', ');  //using Mcrypt_des algorithm, CBC mode        $IV  =  @mcrypt_create_iv (Mcrypt_enc_get_iv_size ($TD),  mcrypt_rand);        $ks  = mcrypt_enc_get_key_size ($TD);       @mcrypt_generic_init ($ td,  $key,  $iv);        //Initial processing        $decrypted  = mdecrypt_generic ( $TD,  $encrypted);        //decryption       mcrypt _generic_deinit ($TD);        //End       mcrypt_ Module_close ($TD);       $y = $this->pkcs5_unpad ($decrypted);       return  $y;   }   function pkcs5_pad  ($text,   $blocksize)  {       $pad  =  $blocksize  -  (strlen ($ Text)  %  $blocksize);      return  $text  . str_repeat (CHR ($pad),  $pad)    }   function pkcs5_unpad ($text)  {        $pad  = ord ($text {strlen ($text)-1})       if   ($pad  >&Nbsp;strlen ($text))  return false;      if  (Strspn ($text, &NBSP;CHR ($pad),  strlen ($text)  -  $pad)  !=  $pad)       return  false;      return substr ($text, 0, -1 *  $pad);    }}


This article is from the "Dongdongのhome" blog, make sure to keep this source http://autophp.blog.51cto.com/8062337/1870570

PHP Curl function to get information about remote host

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.