3242423 发表于 2016-11-9 08:49:49

php curl函数获取远程主机的信息

php程序员开发程序过程中,经常需要调用其他的接口。php为我们提供了一系列函数。curl系列函数。下面就这一些列函数的用法加以说明,以备自己和他人查阅。demo.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
function 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秒超时
    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;
}
// 调用实例
$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);//生成 URL-encode 之后的请求字符串
    }
    $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秒超时
    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;
}
// 调用实例
$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等通用的des加密解密算法类
$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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
/**
* Class Des
* @desc PHP,Java通用的des加密解密算法类
*/
//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');    //本函数用来取得编码方式的区块大小
      $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',''); //使用MCRYPT_DES算法,cbc模式
      $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);       //初始处理
      $decrypted = mdecrypt_generic($td, $encrypted);       //解密
      mcrypt_generic_deinit($td);       //结束
      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 > strlen($text)) return false;
      if (strspn($text, chr($pad), strlen($text) - $pad) != $pad)
      return false;
      return substr($text, 0, -1 * $pad);
   }
}



页: [1]
查看完整版本: php curl函数获取远程主机的信息