92-query-any.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. // $ php examples/92-query-any.php mailbox.org
  3. // $ php examples/92-query-any.php _carddav._tcp.mailbox.org
  4. use React\Dns\Model\Message;
  5. use React\Dns\Model\Record;
  6. use React\Dns\Query\Query;
  7. use React\Dns\Query\TcpTransportExecutor;
  8. use React\EventLoop\Factory;
  9. require __DIR__ . '/../vendor/autoload.php';
  10. $executor = new TcpTransportExecutor('8.8.8.8:53');
  11. $name = isset($argv[1]) ? $argv[1] : 'google.com';
  12. $any = new Query($name, Message::TYPE_ANY, Message::CLASS_IN);
  13. $executor->query($any)->then(function (Message $message) {
  14. foreach ($message->answers as $answer) {
  15. /* @var $answer Record */
  16. $data = $answer->data;
  17. switch ($answer->type) {
  18. case Message::TYPE_A:
  19. $type = 'A';
  20. break;
  21. case Message::TYPE_AAAA:
  22. $type = 'AAAA';
  23. break;
  24. case Message::TYPE_NS:
  25. $type = 'NS';
  26. break;
  27. case Message::TYPE_PTR:
  28. $type = 'PTR';
  29. break;
  30. case Message::TYPE_CNAME:
  31. $type = 'CNAME';
  32. break;
  33. case Message::TYPE_TXT:
  34. // TXT records can contain a list of (binary) strings for each record.
  35. // here, we assume this is printable ASCII and simply concatenate output
  36. $type = 'TXT';
  37. $data = implode('', $data);
  38. break;
  39. case Message::TYPE_MX:
  40. // MX records contain "priority" and "target", only dump its values here
  41. $type = 'MX';
  42. $data = implode(' ', $data);
  43. break;
  44. case Message::TYPE_SRV:
  45. // SRV records contain priority, weight, port and target, dump structure here
  46. $type = 'SRV';
  47. $data = json_encode($data);
  48. break;
  49. case Message::TYPE_SSHFP:
  50. // SSHFP records contain algorithm, fingerprint type and hex fingerprint value
  51. $type = 'SSHFP';
  52. $data = implode(' ', $data);
  53. break;
  54. case Message::TYPE_SOA:
  55. // SOA records contain structured data, dump structure here
  56. $type = 'SOA';
  57. $data = json_encode($data);
  58. break;
  59. case Message::TYPE_CAA:
  60. // CAA records contains flag, tag and value
  61. $type = 'CAA';
  62. $data = $data['flag'] . ' ' . $data['tag'] . ' "' . $data['value'] . '"';
  63. break;
  64. default:
  65. // unknown type uses HEX format
  66. $type = 'TYPE' . $answer->type;
  67. $data = wordwrap(strtoupper(bin2hex($data)), 2, ' ', true);
  68. }
  69. echo $type . ': ' . $data . PHP_EOL;
  70. }
  71. }, 'printf');