---
title: "Avoid Sending SMS to Landlines in PHP with Twilio's Lookup v2 API"
description: "SMS has a 98% open rate and works without internet, but sending to landlines wastes money on failed deliveries. This tutorial demonstrates filtering landlines in PHP using Twilio’s Lookup v2 API, which provides line type intelligence via the “line_type_intelligence” field. The core class extends PHP’s FilterIterator and overrides the accept() method to fetch each number’s line type, returning true only for “mobile” types and catching TwilioException on failure. The application uses Composer dependencies including monolog, twilio/sdk, and phpdotenv, loads credentials from a .env file, iterates over an ArrayIterator of phone numbers, logs each with Monolog to stdout, and sends an SMS only to mobile numbers. This approach avoids unnecessary charges and is straightforward using Twilio’s PHP helper library and SPL iterators."
---

# Avoid Sending SMS to Landlines in PHP with Twilio's Lookup v2 API

[Twilio](https://yomu.fyi/company/twilio) · Matthew Setter · Oct 7, 2026

**Type:** Tutorial

## Summary

SMS has a 98% open rate and works without internet, but sending to landlines wastes money on failed deliveries. This tutorial demonstrates filtering landlines in PHP using Twilio’s Lookup v2 API, which provides line type intelligence via the “line\_type\_intelligence” field. The core class extends PHP’s FilterIterator and overrides the accept() method to fetch each number’s line type, returning true only for “mobile” types and catching TwilioException on failure. The application uses Composer dependencies including monolog, twilio/sdk, and phpdotenv, loads credentials from a .env file, iterates over an ArrayIterator of phone numbers, logs each with Monolog to stdout, and sends an SMS only to mobile numbers. This approach avoids unnecessary charges and is straightforward using Twilio’s PHP helper library and SPL iterators.

## Context

PHP developers sending SMS via Twilio need to avoid charges for failed deliveries to landlines. Twilio’s Lookup v2 API provides line type intelligence to identify mobile numbers. This tutorial shows how to integrate that API with PHP’s SPL iterators to filter phone numbers before sending.

## Approach / What changed

Create a MobilePhoneNumberFilterIterator class that extends PHP’s FilterIterator. In the accept() method, fetch the phone number’s details using Twilio’s Lookup v2 API with the fields parameter set to ‘line\_type\_intelligence’. Return true only if the returned line type is ‘mobile’. Use this iterator with an ArrayIterator of phone numbers and iterate to send SMS only to accepted numbers, logging each step with Monolog.

## Takeaways

- Twilio’s Lookup v2 API can return line type intelligence (e.g., mobile, landline, fixed VoIP) for a phone number by specifying the “fields” parameter as “line\_type\_intelligence”.
- PHP’s FilterIterator class allows filtering elements during iteration; overriding the accept() method with custom logic (here, an API call) filters out unwanted numbers.
- Using Monolog for logging each phone number and whether it was filtered helps track which numbers received SMS and which were skipped.

## FAQ

### How does the Lookup v2 API identify landlines?

The Lookup v2 API returns a 'line\_type\_intelligence' object with a 'type' field that can be 'mobile', 'landline', 'fixed VoIP', etc. The code checks if the type equals 'mobile' to accept the number.

### What happens if the API call fails?

The accept() method catches TwilioException and returns false, filtering out the number. The exception is not logged explicitly in the tutorial, but the number is skipped.

### What dependencies are needed?

The tutorial uses monolog/monolog for logging, twilio/sdk for API access, and vlucas/phpdotenv for loading environment variables from a .env file.

**Tags:** [PHP](https://yomu.fyi/topic/php), [SMS](https://yomu.fyi/topic/sms), [Twilio](https://yomu.fyi/topic/twilio), [Twilio Lookup](https://yomu.fyi/topic/twilio-lookup)

- Source: [Twilio](https://www.twilio.com/en-us/blog/developers/tutorials/avoid-sending-sms-to-landlines-lookup-v2-api-php)
- Source URL: https://www.twilio.com/en-us/blog/developers/tutorials/avoid-sending-sms-to-landlines-lookup-v2-api-php
- Ingested by Yomu: 2026-10-08T03:00:16.538Z

[Read original post](https://www.twilio.com/en-us/blog/developers/tutorials/avoid-sending-sms-to-landlines-lookup-v2-api-php)
