Skip to content

Commit cdc1309

Browse files
committed
Added a curl interface and a simple curl class.
1 parent 277950e commit cdc1309

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/PHPHtmlParser/Curl.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
namespace PHPHtmlParser;
3+
4+
class Curl implements CurlInterface {
5+
6+
/**
7+
* A simple curl implementation to get the content of the url.
8+
*
9+
* @param string $url
10+
* @return string
11+
* @throws Exception
12+
*/
13+
public function get($url)
14+
{
15+
$ch = curl_init($url);
16+
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
17+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
18+
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
19+
20+
$content = curl_exec($ch);
21+
if ($content === false)
22+
{
23+
// there was a problem
24+
$error = curl_error($ch);
25+
throw new Exception('Error retrieving "'.$url.'" ('.$error.')');
26+
}
27+
28+
return $content;
29+
}
30+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
namespace PHPHtmlParser;
3+
4+
interface CurlInterface {
5+
6+
/**
7+
* This method should return the content of the url in a string
8+
*
9+
* @param string $url
10+
* @return string
11+
*/
12+
public function get($url);
13+
}

0 commit comments

Comments
 (0)