PHP version of micro-letter payment api.mch.weixin.qq.com Domain name resolution slow reason and solution _php skills

Source: Internet
Author: User
Tags curl

This article describes the PHP version of micro-letter payment api.mch.weixin.qq.com Domain name resolution slow reasons and solutions. Share to everyone for your reference, specific as follows:

Micro-credit Payment api.mch.weixin.qq.com domain name resolution is slow, resulting in very slow payment, so how to solve the problem of micro-letter payment slow, here to analyze together.

Have friends in the Aliyun host implementation of micro-credit payment logic, found that api.mch.weixin.qq.com parsing is too slow.

So there has been manual modification of the/etc/hosts, of course, which day micro-letter payment if a change in the room must be hung.

Our room also has similar problems, specifically recorded.

The code uses the curl to request the micro-letter, often times out, when using the wget test:

[root@01 tmp]# wget api.mch.weixin.qq.com--2016-06-18 14:51:03--http://api.mch.weixin.qq.com/resolving
Api.mch.weixin.qq.com ...

Domain name resolution A long time not come out

Test confirmation is IPv6 problem

To Wget plus-4, mandatory use of IPv4, if very soon, that is basically determined to be IPv6 caused the trouble.

[root@01 tmp]# wget-4 api.mch.weixin.qq.com--2016-06-18 17:03:52--http://api.mch.weixin.qq.com/resolving
Api.mch.weixin.qq.com ... 123.151.71.149, 123.151.79.109
connecting to api.mch.weixin.qq.com|123.151.71.149|:80 ... connected.

Code Analysis

Specifically write a code to test IPv6 parsing, using the system function Getaddrinfo:

 #include <stdio.h> #include <string.h> #include <netdb.h> #include ;iostream> #include <sys/types.h> #include <sys/types.h> #include <sys/socket.h> #include <
Arpa/inet.h> using namespace std;
  int main () {struct addrinfo hints,*answer,*curr,*p;
  int error;
  memset (&hints, 0, sizeof hints); hints.ai_family = Af_inet6;//af_unspec; Use Af_inet6 to force IPv6 hints.ai_socktype = Sock_stream;//sock_dgram; Sock_stream if ((Error = getaddrinfo ("api.mch.weixin.qq.com", NULL, &hints, &answer))!= 0) {fprintf (std
    Err, "getaddrinfo:%s\n", Gai_strerror (Error));
  return 1;
  else cout << "Success with a url\n";
  Char ipstr[16]; for (Curr = answer; Curr!= NULL; curr = curr->ai_next) {inet_ntop (af_inet,& (struct sockaddr_in *) (curr->
    AI_ADDR))->sin_addr), IPSTR, 16);
  printf ("%s\n", ipstr);
  } freeaddrinfo (answer);
return 0; }

Include header file

Netdb.h

Function Prototypes:

int getaddrinfo (const char hostname, const char service, const struct addrinfo *hints, struct addrinfo);

Parameter description:

Hints: Either a null pointer or a pointer to a ADDRINFO structure in which the caller fills in a hint about the type of information expected to return. For example, if the specified service supports both TCP and UDP, the caller can set the Ai_socktype member in the hints structure to sock_dgram so that only information that is appropriate for the datagram socket interface is returned. And whether IPv6 is decided by ai_family.
Result: This function returns a pointer to a linked list of addrinfo structures through the return pointer argument.

return value:0--successful, non 0--error

Test results:

ai_family for IPv6, will only look for the IPv6 resolution, the general domain name is not set. Ai_family for Af_unspec, will first IPv6 IPv4, and api.mch.weixin.qq.com this domain name IPv6 parse surprisingly slow (qq.com but not slow, after the reason see).

Solution:

If the curl,c can be forced to specify IPv4, use Curl_easy_setopt (Curl, Curlopt_ipresolve, curl_ipresolve_v4);

Other languages also refer to this method.

Deep reason analysis

Nslookup-query=aaaa Api.mch.weixin.qq.com-debug is unable to find the resolution (the specified AAAA is IPv6), Then you will find an SOA statement and his superiors weixin.qq.com has a IPv6 cname, to minorshort.weixin.qq.com, and this domain name is not IPv6 parsing.

Visual IPv6 find resolution is in this SOA and CNAME place dozen circles, micro-letter students are not consider let everyone better, the domain name IPv6 settings removed.

Dig @ns-tel1.qq.com weixin.qq.com AAAA
weixin.qq.com.    43200  in  SOA ns-tel1.qq.com. webmaster.qq.com. 1293502040 300 600 86400 300

For more information on PHP related content readers can view the site topics: "PHP micro-credit Development Skills summary", "PHP coding and transcoding Operation skills Summary", "PHP Network Programming Skills Summary", "PHP basic Grammar Introductory Course", "PHP string (String) Usage Summary", "php+ MySQL Database operations Introduction tutorial and PHP Common database operation Skills Summary

I hope this article will help you with the PHP program design.

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.