|
| 1 | + |
| 2 | + |
| 3 | +# AI Phone Number Detection for Laravel |
| 4 | + |
| 5 | +## 🚀 Leverage AI API to detect and parse phone numbers in text content. |
| 6 | + |
| 7 | +[](https://packagist.org/packages/sharpapi/laravel-content-detect-phones) |
| 8 | +[](https://packagist.org/packages/sharpapi/laravel-content-detect-phones) |
| 9 | + |
| 10 | +Check the details at SharpAPI's [Content API](https://sharpapi.com/en/catalog/ai/content) page. |
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | +## Requirements |
| 15 | + |
| 16 | +- PHP >= 8.1 |
| 17 | +- Laravel >= 9.0 |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +## Installation |
| 22 | + |
| 23 | +Follow these steps to install and set up the SharpAPI Laravel Phone Number Detection package. |
| 24 | + |
| 25 | +1. Install the package via `composer`: |
| 26 | + |
| 27 | +```bash |
| 28 | +composer require sharpapi/laravel-content-detect-phones |
| 29 | +``` |
| 30 | + |
| 31 | +2. Register at [SharpAPI.com](https://sharpapi.com/) to obtain your API key. |
| 32 | + |
| 33 | +3. Set the API key in your `.env` file: |
| 34 | + |
| 35 | +```bash |
| 36 | +SHARP_API_KEY=your_api_key_here |
| 37 | +``` |
| 38 | + |
| 39 | +4. **[OPTIONAL]** Publish the configuration file: |
| 40 | + |
| 41 | +```bash |
| 42 | +php artisan vendor:publish --tag=sharpapi-content-detect-phones |
| 43 | +``` |
| 44 | + |
| 45 | +--- |
| 46 | +## Key Features |
| 47 | + |
| 48 | +- **AI-Powered Phone Number Detection**: Efficiently detect phone numbers in any text content. |
| 49 | +- **E.164 Format Conversion**: Automatically converts detected phone numbers to standardized E.164 format. |
| 50 | +- **Multiple Number Detection**: Identifies all phone numbers present in the provided text. |
| 51 | +- **International Number Support**: Recognizes phone numbers from different countries and formats. |
| 52 | +- **Robust Polling for Results**: Polling-based API response handling with customizable intervals. |
| 53 | +- **API Availability and Quota Check**: Check API availability and current usage quotas with SharpAPI's endpoints. |
| 54 | + |
| 55 | +--- |
| 56 | + |
| 57 | +## Usage |
| 58 | + |
| 59 | +You can inject the `ContentDetectPhonesService` class to access phone number detection functionality. For best results, especially with batch processing, use Laravel's queuing system to optimize job dispatch and result polling. |
| 60 | + |
| 61 | +### Basic Workflow |
| 62 | + |
| 63 | +1. **Dispatch Job**: Send text content to the API using `detectPhones`, which returns a status URL. |
| 64 | +2. **Poll for Results**: Use `fetchResults($statusUrl)` to poll until the job completes or fails. |
| 65 | +3. **Process Result**: After completion, retrieve the results from the `SharpApiJob` object returned. |
| 66 | + |
| 67 | +> **Note**: Each job typically takes a few seconds to complete. Once completed successfully, the status will update to `success`, and you can process the results as JSON, array, or object format. |
| 68 | +
|
| 69 | +--- |
| 70 | + |
| 71 | +### Controller Example |
| 72 | + |
| 73 | +Here is an example of how to use `ContentDetectPhonesService` within a Laravel controller: |
| 74 | + |
| 75 | +```php |
| 76 | +<?php |
| 77 | + |
| 78 | +namespace App\Http\Controllers; |
| 79 | + |
| 80 | +use GuzzleHttp\Exception\GuzzleException; |
| 81 | +use SharpAPI\ContentDetectPhones\ContentDetectPhonesService; |
| 82 | + |
| 83 | +class ContentController extends Controller |
| 84 | +{ |
| 85 | + protected ContentDetectPhonesService $phoneDetectionService; |
| 86 | + |
| 87 | + public function __construct(ContentDetectPhonesService $phoneDetectionService) |
| 88 | + { |
| 89 | + $this->phoneDetectionService = $phoneDetectionService; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @throws GuzzleException |
| 94 | + */ |
| 95 | + public function detectPhoneNumbers(string $text) |
| 96 | + { |
| 97 | + $statusUrl = $this->phoneDetectionService->detectPhones($text); |
| 98 | + |
| 99 | + $result = $this->phoneDetectionService->fetchResults($statusUrl); |
| 100 | + |
| 101 | + return response()->json($result->getResultJson()); |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +### Handling Guzzle Exceptions |
| 107 | + |
| 108 | +All requests are managed by Guzzle, so it's helpful to be familiar with [Guzzle Exceptions](https://docs.guzzlephp.org/en/stable/quickstart.html#exceptions). |
| 109 | + |
| 110 | +Example: |
| 111 | + |
| 112 | +```php |
| 113 | +use GuzzleHttp\Exception\ClientException; |
| 114 | + |
| 115 | +try { |
| 116 | + $statusUrl = $this->phoneDetectionService->detectPhones('Contact us at +1 (800) 555-1234 or 555-6789'); |
| 117 | +} catch (ClientException $e) { |
| 118 | + echo $e->getMessage(); |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +--- |
| 123 | + |
| 124 | +## Optional Configuration |
| 125 | + |
| 126 | +You can customize the configuration by setting the following environment variables in your `.env` file: |
| 127 | + |
| 128 | +```bash |
| 129 | +SHARP_API_KEY=your_api_key_here |
| 130 | +SHARP_API_JOB_STATUS_POLLING_WAIT=180 |
| 131 | +SHARP_API_JOB_STATUS_USE_POLLING_INTERVAL=true |
| 132 | +SHARP_API_JOB_STATUS_POLLING_INTERVAL=10 |
| 133 | +SHARP_API_BASE_URL=https://sharpapi.com/api/v1 |
| 134 | +``` |
| 135 | + |
| 136 | +--- |
| 137 | + |
| 138 | +## Phone Number Detection Data Format Example |
| 139 | + |
| 140 | +```json |
| 141 | +{ |
| 142 | + "data": { |
| 143 | + "type": "api_job_result", |
| 144 | + "id": "d43b36dc-3d1d-4ba7-9a17-36a438d91f09", |
| 145 | + "attributes": { |
| 146 | + "status": "success", |
| 147 | + "type": "content_detect_phones", |
| 148 | + "result": [ |
| 149 | + { |
| 150 | + "parsed_number": "+18003947486", |
| 151 | + "detected_number": "1800-394-7486" |
| 152 | + }, |
| 153 | + { |
| 154 | + "parsed_number": "+6588888888", |
| 155 | + "detected_number": "+65 8888 8888" |
| 156 | + } |
| 157 | + ] |
| 158 | + } |
| 159 | + } |
| 160 | +} |
| 161 | +``` |
| 162 | + |
| 163 | +--- |
| 164 | + |
| 165 | +## Support & Feedback |
| 166 | + |
| 167 | +For issues or suggestions, please: |
| 168 | + |
| 169 | +- [Open an issue on GitHub](https://github.com/sharpapi/laravel-content-detect-phones/issues) |
| 170 | +- Join our [Telegram community](https://t.me/sharpapi_community) |
| 171 | + |
| 172 | +--- |
| 173 | + |
| 174 | +## Changelog |
| 175 | + |
| 176 | +Please see [CHANGELOG](CHANGELOG.md) for a detailed list of changes. |
| 177 | + |
| 178 | +--- |
| 179 | + |
| 180 | +## Credits |
| 181 | + |
| 182 | +- [A2Z WEB LTD](https://github.com/a2zwebltd) |
| 183 | +- [Dawid Makowski](https://github.com/makowskid) |
| 184 | +- Enhance your [Laravel AI](https://sharpapi.com/) capabilities! |
| 185 | + |
| 186 | +--- |
| 187 | + |
| 188 | +## License |
| 189 | + |
| 190 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
| 191 | + |
| 192 | +--- |
| 193 | + |
| 194 | +## Follow Us |
| 195 | + |
| 196 | +Stay updated with news, tutorials, and case studies: |
| 197 | + |
| 198 | +- [SharpAPI on X (Twitter)](https://x.com/SharpAPI) |
| 199 | +- [SharpAPI on YouTube](https://www.youtube.com/@SharpAPI) |
| 200 | +- [SharpAPI on Vimeo](https://vimeo.com/SharpAPI) |
| 201 | +- [SharpAPI on LinkedIn](https://www.linkedin.com/products/a2z-web-ltd-sharpapicom-automate-with-aipowered-api/) |
| 202 | +- [SharpAPI on Facebook](https://www.facebook.com/profile.php?id=61554115896974) |
0 commit comments