Skip to content

Commit f728b05

Browse files
committed
feat: add files
1 parent b2fb5c1 commit f728b05

File tree

11 files changed

+2364
-0
lines changed

11 files changed

+2364
-0
lines changed

examples/README.md

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# ScrapeGraphAI PHP SDK Examples
2+
3+
This directory contains comprehensive examples demonstrating how to use the ScrapeGraphAI PHP SDK for various web scraping and data extraction tasks.
4+
5+
## Prerequisites
6+
7+
Before running the examples, make sure you have:
8+
9+
1. **PHP 8.1+** installed
10+
2. **Composer** installed
11+
3. **ScrapeGraphAI API Key** - Get one from [scrapegraphai.com](https://scrapegraphai.com)
12+
13+
## Setup
14+
15+
1. Install dependencies:
16+
```bash
17+
composer install
18+
```
19+
20+
2. Set your API key as an environment variable:
21+
```bash
22+
export SCRAPEGRAPHAI_API_KEY="your-api-key-here"
23+
```
24+
25+
Or create a `.env` file in the examples directory:
26+
```
27+
SCRAPEGRAPHAI_API_KEY=your-api-key-here
28+
```
29+
30+
## Examples Overview
31+
32+
### Basic Examples
33+
- **[basic-smartscraper.php](basic/basic-smartscraper.php)** - Simple web page scraping
34+
- **[basic-markdownify.php](basic/basic-markdownify.php)** - Convert web pages to Markdown
35+
- **[basic-searchscraper.php](basic/basic-searchscraper.php)** - Search and scrape multiple websites
36+
- **[basic-crawl.php](basic/basic-crawl.php)** - Website crawling
37+
- **[basic-schema.php](basic/basic-schema.php)** - Generate JSON schemas
38+
- **[basic-credits.php](basic/basic-credits.php)** - Check API credits
39+
- **[basic-validate.php](basic/basic-validate.php)** - Validate API key
40+
41+
### Advanced Examples
42+
- **[advanced-smartscraper.php](advanced/advanced-smartscraper.php)** - Advanced scraping with custom schemas
43+
- **[advanced-crawl.php](advanced/advanced-crawl.php)** - Complex crawling with rules and filters
44+
- **[advanced-error-handling.php](advanced/advanced-error-handling.php)** - Comprehensive error handling
45+
- **[advanced-pagination.php](advanced/advanced-pagination.php)** - Handle paginated content
46+
- **[advanced-javascript.php](advanced/advanced-javascript.php)** - Scrape JavaScript-heavy sites
47+
48+
### Real-World Use Cases
49+
- **[ecommerce-scraper.php](use-cases/ecommerce-scraper.php)** - E-commerce product scraping
50+
- **[news-aggregator.php](use-cases/news-aggregator.php)** - News article extraction
51+
- **[social-media.php](use-cases/social-media.php)** - Social media content scraping
52+
- **[job-listings.php](use-cases/job-listings.php)** - Job board scraping
53+
- **[real-estate.php](use-cases/real-estate.php)** - Real estate listing scraping
54+
55+
## Running Examples
56+
57+
To run any example:
58+
59+
```bash
60+
cd examples
61+
php basic/basic-smartscraper.php
62+
```
63+
64+
## Common Patterns
65+
66+
### 1. Basic Client Setup
67+
```php
68+
<?php
69+
require_once '../vendor/autoload.php';
70+
71+
use Scrapegraphai\Client;
72+
73+
$client = new Client(
74+
apiKey: getenv('SCRAPEGRAPHAI_API_KEY')
75+
);
76+
```
77+
78+
### 2. Error Handling
79+
```php
80+
use Scrapegraphai\Errors\APIError;
81+
use Scrapegraphai\Errors\RateLimitError;
82+
83+
try {
84+
$result = $client->smartscraper->create($params);
85+
} catch (RateLimitError $e) {
86+
echo "Rate limit exceeded. Please wait before retrying.\n";
87+
} catch (APIError $e) {
88+
echo "API Error: " . $e->getMessage() . "\n";
89+
}
90+
```
91+
92+
### 3. Async Operations
93+
Many operations are asynchronous. You'll need to poll for results:
94+
95+
```php
96+
// Start the operation
97+
$response = $client->smartscraper->create($params);
98+
$requestId = $response->request_id;
99+
100+
// Poll for completion
101+
do {
102+
sleep(2);
103+
$result = $client->smartscraper->retrieve($requestId);
104+
} while ($result->status === 'processing');
105+
```
106+
107+
## Tips
108+
109+
- Always handle errors appropriately
110+
- Use structured output schemas for consistent data extraction
111+
- Be mindful of rate limits
112+
- Cache results when possible to save credits
113+
- Use the most specific scraper for your use case:
114+
- **SmartScraper**: Single page extraction
115+
- **SearchScraper**: Search and extract from multiple sources
116+
- **Crawler**: Full website crawling
117+
- **Markdownify**: Convert to clean Markdown format
118+
119+
## Support
120+
121+
If you encounter issues with these examples:
122+
123+
1. Check your API key is correctly set
124+
2. Ensure you have sufficient credits
125+
3. Review the [official documentation](https://scrapegraphai.com)
126+
4. File an issue on [GitHub](https://github.com/stainless-sdks/scrapegraphai-php/issues)

0 commit comments

Comments
 (0)