Skip to content

Commit 45af332

Browse files
committed
Updated documentation
1 parent cdc1309 commit 45af332

File tree

3 files changed

+81
-4
lines changed

3 files changed

+81
-4
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,36 @@ This example loads the html from big.html, a real page found online, and gets al
5757

5858
Alternativly, you can always use the load() method to load the file. It will attempt to find the file using file_exists and, if succesfull, will call loadFromFile() for you. The same applies to a URL and loadFromUrl() method.
5959

60+
Example With Url
61+
----------------
62+
63+
Loading a url is very similar to the way you would load the html from a file.
64+
65+
```php
66+
use PHPHtmlParser\Dom;
67+
68+
$dom = new Dom;
69+
$dom->loadFromUrl('http://google.com');
70+
$html = $dom->outerHtml;
71+
72+
// or
73+
$dom->load('http://google.com');
74+
$html = $dom->outerHtml; // same result as the first example
75+
```
76+
77+
What makes the loadFromUrl method note worthy is the PHPHtmlParser\CurlInterface parameter, an optional second parameter. By default, we use the PHPHtmlParser\Curl class to get the contents of the url. On the other hand, though, you can inject your own implementation of CurlInterface and we will attempt to load the url using what ever tool/settings you want, up to you.
78+
79+
```php
80+
use PHPHtmlParser\Dom;
81+
use App\Services\Connector;
82+
83+
$dom = new Dom;
84+
$dom->loadFromUrl('http://google.com', new Connector);
85+
$html = $dom->outerHtml;
86+
```
87+
88+
As long as the Connector object implements the PHPHtmlParser\CurlInterface interface properly it will use that object to get the content of the url instead of the default PHPHtmlParser\Curl class.
89+
6090
Static Facade
6191
------------
6292

src/PHPHtmlParser/Dom.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ public function loadFromFile($file)
122122
}
123123

124124
/**
125-
* Uses guzzle to load the html from the given url.
125+
* Use a curl interface implementation to attempt to load
126+
* the content from a url.
126127
*
127128
* @param string $url
128129
* @param CurlInterface $curl

src/PHPHtmlParser/StaticDom.php

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ final class StaticDom {
55

66
private static $dom = null;
77

8+
/**
9+
* Attempts to call the given method on the most recent created dom
10+
* from bellow.
11+
*
12+
* @param string $method
13+
* @param array $arguments
14+
* @throws Exception
15+
* @return mixed
16+
*/
817
public static function __callStatic($method, $arguments)
918
{
1019
if (self::$dom instanceof Dom)
@@ -17,6 +26,14 @@ public static function __callStatic($method, $arguments)
1726
}
1827
}
1928

29+
/**
30+
* Call this to mount the static facade. The facade allows you to use
31+
* this object as a $className.
32+
*
33+
* @param string $className
34+
* @param Dom $dom
35+
* @return bool
36+
*/
2037
public static function mount($className = 'Dom', Dom $dom = null)
2138
{
2239
if (class_exists($className))
@@ -31,23 +48,52 @@ class_alias(__CLASS__, $className);
3148
return true;
3249
}
3350

51+
/**
52+
* Creates a new dom object and calls load() on the
53+
* new object.
54+
*
55+
* @param string $str
56+
* @chainable
57+
*/
3458
public static function load($str)
3559
{
3660
$dom = new Dom;
3761
self::$dom = $dom;
3862
return $dom->load($str);
3963
}
4064

65+
/**
66+
* Creates a new dom object and calls loadFromFile() on the
67+
* new object.
68+
69+
* @param string $file
70+
* @chainable
71+
*/
4172
public static function loadFromFile($file)
4273
{
43-
$dom = new Dom;
74+
$dom = new Dom;
75+
self::$dom = $dom;
4476
return $dom->loadFromFile($file);
4577
}
4678

47-
public static function loadFromUrl($url)
79+
/**
80+
* Creates a new dom object and calls loadFromUrl() on the
81+
* new object.
82+
*
83+
* @param string $url
84+
* @param CurlInterface $curl
85+
* @chainable
86+
*/
87+
public static function loadFromUrl($url, CurlInterface $curl = null)
4888
{
4989
$dom = new Dom;
5090
self::$dom = $dom;
51-
return $dom->loadFromUrl($url);
91+
if (is_null($curl))
92+
{
93+
// use the default curl interface
94+
$curl = new Curl;
95+
}
96+
97+
return $dom->loadFromUrl($url, $curl);
5298
}
5399
}

0 commit comments

Comments
 (0)