开发者

Finding the phone company of a cell phone number?

I have an application where people can give a phone number and it will send SMS texts to the phone number through EMail-SMS gateways. For this to work however, I need the phone company of the given number so that I send the email to the proper SMS gateway. I've seen some services that allow you to look up this information, but none of them in the form of a web service or database.

For instance, http://tnid.us provides such a service. Example 开发者_如何学JAVAoutput from my phone number:

Finding the phone company of a cell phone number?

Where do they get the "Current Telephone Company" information for each number. Is this freely available information? Is there a database or some sort of web service I can use to get that information for a given cell phone number?


What you need is called a HLR (Home Location Register) number lookup.

In their basic forms such APIs will expect a phone number in international format (example, +15121234567) and will return back their IMSI, which includes their MCC (gives you the country) and MNC (gives you the phone's carrier). The may even include the phone's current carrier (eg to tell if the phone is roaming). It may not work if the phone is currently out of range or turned off. In those cases, depending on the API provider, they may give you a cached result.

The site you mentioned seems to provide such functionality. A web search for "HLR lookup API" will give you plenty more results. I have personal experience with CLX's service and would recommend it.


This would be pretty code intensive, but something you could do right now, on your own, without APIs as long as the tnid.us site is around:

Why not have IE open in a hidden browser window with the URL of the phone number? It looks like the URL would take the format of http://tnid.us/search.php?q=########## where # represents a number. So you need a textbox, a label, and a button. I call the textbox "txtPhoneNumber", the label "lblCarrier", and the button would call the function I have below "OnClick".

The button function creates the IE instance using MSHtml.dll and SHDocVW.dll and does a page scrape of the HTML that is in your browser "object". You then parse it down. You have to first install the Interoperability Assemblies that came with Visual Studio 2005 (C:\Program Files\Common Files\Merge Modules\vs_piaredist.exe). Then:

1> Create a new web project in Visual Studio.NET.

2> Add a reference to SHDocVw.dll and Microsoft.mshtml.

3> In default.aspx.cs, add these lines at the top:

using mshtml;
using SHDocVw;
using System.Threading;

4> Add the following function :

protected void executeMSIE(Object sender, EventArgs e)
{
    SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorerClass();
    object o = System.Reflection.Missing.Value;
    TextBox txtPhoneNumber = (TextBox)this.Page.FindControl("txtPhoneNumber");
    object url = "http://tnid.us/search.php?q=" + txtPhoneNumber.Text);
    StringBuilder sb = new StringBuilder();

    if (ie != null) {
        ie.Navigate2(ref url,ref o,ref o,ref o,ref o);
        ie.Visible = false;
        while(ie.Busy){Thread.Sleep(2);}
        IHTMLDocument2 d = (IHTMLDocument2) ie.Document;
        if (d != null) {
            IHTMLElementCollection all = d.all;
            string ourText = String.Empty;
            foreach (object el in all)
            {
               //find the text by checking each (string)el.Text
               if ((string)el.ToString().Contains("Current Phone Company"))
                   ourText = (string)el.ToString();
            }
         // or maybe do something like this instead of the loop above...
         // HTMLInputElement searchText = (HTMLInputElement)d.all.item("p", 0);
            int idx = 0;
            // and do a foreach on searchText to find the right "<p>"...
            foreach (string s in searchText) {
               if (s.Contains("Current Phone Company") || s.Contains("Original Phone Company")) {
                  idx = s.IndexOf("<strong>") + 8;
                  ourText = s.Substring(idx);
                  idx = ourText.IndexOf('<');
                  ourText = ourText.Substring(0, idx); 
               }
            }
            // ... then decode "ourText"
            string[] ourArray = ourText.Split(';');
            foreach (string s in ourArray) {
                char c = (char)s.Split('#')[1];
                sb.Append(c.ToString());
            }
            // sb.ToString() is now your phone company carrier....
        }
    }

    if (sb != null)
        lblCarrier.Text = sb.ToString();
    else
        lblCarrier.Text = "No MSIE?";
}

For some reason I don't get the "Current Phone Company" when I just use the tnid.us site directly, though, only the Original. So you might want to have the code test what it's getting back, i.e.

bool currentCompanyFound = false;
if (s.Contains("Current Telephone Company")) {  currentCompanyFound = true }

I have it checking for either one, above, so you get something back. What the code should do is to find the area of HTML between

<p class="lt">Current Telephone Company:<br /><strong>

and

</strong></p>

I have it looking for the index of

<strong>

and adding on the characters of that word to get to the starting position. I can't remember if you can use strings or only characters for .indexOf. But you get the point and you or someone else can probably find a way to get it working from there.

That text you get back is encoded with char codes, so you'd have to convert those. I gave you some code above that should assist in that... it's untested and completely from my head, but it should work or get you where you're going.


Did you look just slightly farther down on the tnid.us result page?

Need API access?  Contact sales@tnID.us.


[Disclosure: I work for Twilio]

You can retrieve phone number information with Twilio Lookup.

If you are currently evaluating services and functionality for phone number lookup, I'd suggest giving Lookup a try via the quickstart.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜