url_api.inc.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. $gj_config = array(
  3. 'url_server' => 'http://openapi.guanjia.qq.com/fcgi-bin/urlquery?appid=10003&query_type=1&host=' . $_SERVER["HTTP_HOST"],
  4. 'appid' => 10003,
  5. 'appkey' => '006556a253d9f4ac540c53a4734fbfc2'
  6. );
  7. main($gj_config);
  8. function main($gj_config){
  9. $data = explode('|', $_POST['content']);
  10. array_remove_empty($data);
  11. $url_num = count($data);
  12. $post_data = '';
  13. foreach($data as $info){
  14. $tmp = explode('^', $info);
  15. if(empty($tmp[0])) continue;
  16. $post_data .= 'urlinfo=' . urlencode(base64_encode('url=' . urlencode($tmp[0]) . '&urlSeq=' . $tmp[1])) . '&';
  17. }
  18. echo 'gj_plugin_function.gettype_callback_a(' . check_url($gj_config, $post_data, $url_num) . ')';
  19. }
  20. function get_appsig($config, $data){
  21. $data_len = strlen($data);
  22. $str_md5 = md5(pack('I', $config['appid']) . $config['appkey'] . pack('I', $data_len));
  23. $buffer1 = $str_md5 . pack('I', $data_len);
  24. $buffer2 = pack('I', $config['appid']) . $config['appkey'];
  25. $sig = $buffer1 ^ $buffer2;
  26. return urlencode(base64_encode($sig));
  27. }
  28. function check_url($config, $data, $url_num){
  29. $server = $config['url_server'] . '&appsig=' . get_appsig($config, $data);
  30. return connect($server, $data, $url_num);
  31. }
  32. function connect($server, $data, $url_num){
  33. $context = array(
  34. 'http'=>array(
  35. 'method'=> 'POST',
  36. 'header'=> 'Content-Type: application/x-www-form-urlencoded'."\r\n",
  37. 'content'=> $data
  38. )
  39. );
  40. $stream_context = stream_context_create($context);
  41. $ret = file_get_contents($server . '&urlNum=' . $url_num, FALSE, $stream_context);
  42. return $ret;
  43. }
  44. function array_remove_empty(& $arr, $trim = true){
  45. foreach ($arr as $key => $value) {
  46. if (is_array($value)) {
  47. array_remove_empty($arr[$key]);
  48. } else {
  49. $value = trim($value);
  50. if ($value == '') {
  51. unset($arr[$key]);
  52. } elseif ($trim) {
  53. $arr[$key] = $value;
  54. }
  55. }
  56. }
  57. }
  58. ?>