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