переделал по другому - скачал с github готовый класс
| PHP | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
| <?php
/**
* Sitemap
*
* This class used for generating Google Sitemap files
*
* @package Sitemap
* @author Osman Üngür <osmanungur@gmail.com>
* @copyright 2009-2011 Osman Üngür
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version Version @package_version@
* @since Class available since Version 1.0.0
* @link http://github.com/osmanungur/sitemap-php
*/
class Sitemap {
/**
*
* @var XMLWriter
*/
private $writer;
private $domain;
private $path;
private $filename = 'sitemap';
private $current_item = 0;
private $current_sitemap = 0;
const EXT = '.xml';
const SCHEMA = 'http://www.sitemaps.org/schemas/sitemap/0.9';
const DEFAULT_PRIORITY = 0.5;
const ITEM_PER_SITEMAP = 50000;
const SEPERATOR = '-';
const INDEX_SUFFIX = 'index';
/**
*
* @param string $domain
*/
public function __construct($domain) {
$this->setDomain($domain);
}
/**
* Sets root path of the website, starting with http:// or https://
*
* @param string $domain
*/
public function setDomain($domain) {
$this->domain = $domain;
return $this;
}
/**
* Returns root path of the website
*
* @return string
*/
private function getDomain() {
return $this->domain;
}
/**
* Returns XMLWriter object instance
*
* @return XMLWriter
*/
private function getWriter() {
return $this->writer;
}
/**
* Assigns XMLWriter object instance
*
* @param XMLWriter $writer
*/
private function setWriter(XMLWriter $writer) {
$this->writer = $writer;
}
/**
* Returns path of sitemaps
*
* @return string
*/
private function getPath() {
return $this->path;
}
/**
* Sets paths of sitemaps
*
* @param string $path
* @return Sitemap
*/
public function setPath($path) {
$this->path = $path;
return $this;
}
/**
* Returns filename of sitemap file
*
* @return string
*/
private function getFilename() {
return $this->filename;
}
/**
* Sets filename of sitemap file
*
* @param string $filename
* @return Sitemap
*/
public function setFilename($filename) {
$this->filename = $filename;
return $this;
}
/**
* Returns current item count
*
* @return int
*/
private function getCurrentItem() {
return $this->current_item;
}
/**
* Increases item counter
*
*/
private function incCurrentItem() {
$this->current_item = $this->current_item + 1;
}
/**
* Returns current sitemap file count
*
* @return int
*/
private function getCurrentSitemap() {
return $this->current_sitemap;
}
/**
* Increases sitemap file count
*
*/
private function incCurrentSitemap() {
$this->current_sitemap = $this->current_sitemap + 1;
}
/**
* Prepares sitemap XML document
*
*/
private function startSitemap() {
$this->setWriter(new XMLWriter());
#$this->getWriter()->openURI($this->getPath() . $this->getFilename() . self::SEPERATOR . $this->getCurrentSitemap() . self::EXT);
$this->getWriter()->openURI($this->getPath() . $this->getFilename() . self::EXT);
$this->getWriter()->startDocument('1.0', 'UTF-8');
$this->getWriter()->setIndent(true);
$this->getWriter()->startElement('urlset');
$this->getWriter()->writeAttribute('xmlns', self::SCHEMA);
}
/**
* Adds an item to sitemap
*
* @param string $loc URL of the page. This value must be less than 2,048 characters.
* @param string $priority The priority of this URL relative to other URLs on your site. Valid values range from 0.0 to 1.0.
* @param string $changefreq How frequently the page is likely to change. Valid values are always, hourly, daily, weekly, monthly, yearly and never.
* @param string|int $lastmod The date of last modification of url. Unix timestamp or any English textual datetime description.
* @return Sitemap
*/
public function addItem($loc, $priority = self::DEFAULT_PRIORITY, $changefreq = NULL, $lastmod = NULL) {
if (($this->getCurrentItem() % self::ITEM_PER_SITEMAP) == 0) {
if ($this->getWriter() instanceof XMLWriter) {
$this->endSitemap();
}
$this->startSitemap();
$this->incCurrentSitemap();
}
$this->incCurrentItem();
$this->getWriter()->startElement('url');
$this->getWriter()->writeElement('loc', $this->getDomain() . $loc);
$this->getWriter()->writeElement('priority', $priority);
if ($changefreq)
$this->getWriter()->writeElement('changefreq', $changefreq);
if ($lastmod)
$this->getWriter()->writeElement('lastmod', $this->getLastModifiedDate($lastmod));
$this->getWriter()->endElement();
return $this;
}
/**
* Prepares given date for sitemap
*
* @param string $date Unix timestamp or any English textual datetime description
* @return string Year-Month-Day formatted date.
*/
private function getLastModifiedDate($date) {
if (ctype_digit($date)) {
return date('Y-m-d', $date);
} else {
$date = strtotime($date);
return date('Y-m-d', $date);
}
}
/**
* Finalizes tags of sitemap XML document.
*
*/
private function endSitemap() {
$this->getWriter()->endElement();
$this->getWriter()->endDocument();
}
/**
* Writes Google sitemap index for generated sitemap files
*
* @param string $loc Accessible URL path of sitemaps
* @param string|int $lastmod The date of last modification of sitemap. Unix timestamp or any English textual datetime description.
*/
public function createSitemapIndex($loc, $lastmod = 'Today') {
$this->endSitemap();
$indexwriter = new XMLWriter();
$indexwriter->openURI($this->getPath() . $this->getFilename() . self::SEPERATOR . self::INDEX_SUFFIX . self::EXT);
$indexwriter->startDocument('1.0', 'UTF-8');
$indexwriter->setIndent(true);
$indexwriter->startElement('sitemapindex');
$indexwriter->writeAttribute('xmlns', self::SCHEMA);
for ($index = 0; $index < $this->getCurrentSitemap(); $index++) {
$indexwriter->startElement('sitemap');
$indexwriter->writeElement('loc', $loc . $this->getFilename() . self::SEPERATOR . $index . self::EXT);
$indexwriter->writeElement('lastmod', $this->getLastModifiedDate($lastmod));
$indexwriter->endElement();
}
$indexwriter->endElement();
$indexwriter->endDocument();
}
} |
|
вызываю его у себя так:
| PHP | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| //подключаем класс
include 'sitemap.php';
//создаем новую карту
$sitemap = new Sitemap('http://www.сайт.ru');
//указываем путь, куда сохранится сгенерированный .xml
$sitemap->setPath('/');
//задаем имя нашей "карте сайта".xml
$sitemap->setFilename('sitemap');
$sitemap->addItem('', '1', 'weekly', 'Today');
$query1 = "SELECT * from news order by p";
$result1 = mysql_query ($query1, $link);
while ($row1 = mysql_fetch_object ($result1))
{
$urls1 = "/service/".$row1->keyer;
$sitemap->addItem($urls1, '0.6', 'weekly', 'Today');
}
//указываем домен
$sitemap->setDomain('http://www.сайт.ru'); |
|
почти все классно, все формирует - правдо формирует то что задано в $sitemap->addItem, как получить все внутренние url'ы??? у меня они просто прописаны в ручную, а некоторые из базы берутся, но лишь часть, можно как нибудь подтянуть все url's через сторонний ресурс, например с помощью cURL?
Добавлено через 4 часа 28 минут
| PHP | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
| <?
class sitemap
{
private $sitemap_urls = array();
private $base;
private $protocol;
private $domain;
private $check = array();
private $proxy = "";
//setting list of substring to ignore in urls
public function set_ignore($ignore_list){
$this->check = $ignore_list;
}
//set a proxy host and port (such as someproxy:8080 or 10.1.1.1:8080
public function set_proxy($host_port){
$this->proxy = $host_port;
}
//validating urls using list of substrings
private function validate($url){
$valid = true;
//add substrings of url that you don't want to appear using set_ignore() method
foreach($this->check as $val)
{
if(stripos($url, $val) !== false)
{
$valid = false;
break;
}
}
return $valid;
}
//multi curl requests
private function multi_curl($urls){
// for curl handlers
$curl_handlers = array();
//setting curl handlers
foreach ($urls as $url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
if (isset($this->proxy) && !$this->proxy == '')
{
curl_setopt($curl, CURLOPT_PROXY, $this->proxy);
}
$curl_handlers[] = $curl;
}
//initiating multi handler
$multi_curl_handler = curl_multi_init();
// adding all the single handler to a multi handler
foreach($curl_handlers as $key => $curl)
{
curl_multi_add_handle($multi_curl_handler,$curl);
}
// executing the multi handler
do
{
$multi_curl = curl_multi_exec($multi_curl_handler, $active);
}
while ($multi_curl == CURLM_CALL_MULTI_PERFORM || $active);
foreach($curl_handlers as $curl)
{
//checking for errors
if(curl_errno($curl) == CURLE_OK)
{
//if no error then getting content
$content = curl_multi_getcontent($curl);
//parsing content
$this->parse_content($content);
}
}
curl_multi_close($multi_curl_handler);
return true;
}
//function to call
public function get_links($domain){
//getting base of domain url address
$this->base = str_replace("http://", "", $domain);
$this->base = str_replace("https://", "", $this->base);
$host = explode("/", $this->base);
$this->base = $host[0];
//getting proper domain name and protocol
$this->domain = trim($domain);
if(strpos($this->domain, "http") !== 0)
{
$this->protocol = "http://";
$this->domain = $this->protocol.$this->domain;
}
else
{
$protocol = explode("//", $domain);
$this->protocol = $protocol[0]."//";
}
if(!in_array($this->domain, $this->sitemap_urls))
{
$this->sitemap_urls[] = $this->domain;
}
//requesting link content using curl
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $this->domain);
if (isset($this->proxy) && !$this->proxy == '')
{
curl_setopt($curl, CURLOPT_PROXY, $this->proxy);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($curl);
curl_close($curl);
$this->parse_content($page);
}
//parses content and checks for URLs
private function parse_content($page){
//getting all links from href attributes
preg_match_all("/<a[^>]*href\s*=\s*'([^']*)'|".
'<a[^>]*href\s*=\s*"([^"]*)"'."/is", $page, $match);
//storing new links
$new_links = array();
for($i = 1; $i < sizeof($match); $i++)
{
//walking through links
foreach($match[$i] as $url)
{
//if doesn't start with http and is not empty
if(strpos($url, "http") === false && trim($url) !== "")
{
//checking if absolute path
if($url[0] == "/") $url = substr($url, 1);
//checking if relative path
else if($url[0] == ".")
{
while($url[0] != "/")
{
$url = substr($url, 1);
}
$url = substr($url, 1);
}
//transforming to absolute url
$url = $this->protocol.$this->base."/".$url;
}
//if new and not empty
if(!in_array($url, $this->sitemap_urls) && trim($url) !== "")
{
//if valid url
if($this->validate($url))
{
//checking if it is url from our domain
if(strpos($url, "http://".$this->base) === 0 || strpos($url, "https://".$this->base) === 0)
{
//adding url to sitemap array
$this->sitemap_urls[] = $url;
//adding url to new link array
$new_links[] = $url;
}
}
}
}
}
$this->multi_curl($new_links);
return true;
}
//returns array of sitemap URLs
public function get_array(){
return $this->sitemap_urls;
}
//notifies services like google, bing, yahoo, ask and moreover about your site map update
public function ping($sitemap_url, $title ="", $siteurl = ""){
// for curl handlers
$curl_handlers = array();
$sitemap_url = trim($sitemap_url);
if(strpos($sitemap_url, "http") !== 0)
{
$sitemap_url = "http://".$sitemap_url;
}
$site = explode("//", $sitemap_url);
$start = $site[0];
$site = explode("/", $site[1]);
$middle = $site[0];
if(trim($title) == "")
{
$title = $middle;
}
if(trim($siteurl) == "")
{
$siteurl = $start."//".$middle;
}
//urls to ping
$urls[0] = "http://www.google.com/webmasters/tools/ping?sitemap=".urlencode($sitemap_url);
$urls[1] = "http://www.bing.com/webmaster/ping.aspx?siteMap=".urlencode($sitemap_url);
$urls[2] = "http://search.yahooapis.com/SiteExplorerService/V1/updateNotification".
"?appid=YahooDemo&url=".urlencode($sitemap_url);
$urls[3] = "http://submissions.ask.com/ping?sitemap=".urlencode($sitemap_url);
$urls[4] = "http://rpc.weblogs.com/pingSiteForm?name=".urlencode($title).
"&url=".urlencode($siteurl)."&changesURL=".urlencode($sitemap_url);
//setting curl handlers
foreach ($urls as $url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURL_HTTP_VERSION_1_1, 1);
$curl_handlers[] = $curl;
}
//initiating multi handler
$multi_curl_handler = curl_multi_init();
// adding all the single handler to a multi handler
foreach($curl_handlers as $key => $curl)
{
curl_multi_add_handle($multi_curl_handler,$curl);
}
// executing the multi handler
do
{
$multi_curl = curl_multi_exec($multi_curl_handler, $active);
}
while ($multi_curl == CURLM_CALL_MULTI_PERFORM || $active);
// check if there any error
$submitted = true;
foreach($curl_handlers as $key => $curl)
{
//you may use curl_multi_getcontent($curl); for getting content
//and curl_error($curl); for getting errors
if(curl_errno($curl) != CURLE_OK)
{
$submitted = false;
}
}
curl_multi_close($multi_curl_handler);
return $submitted;
}
//generates sitemap
public function generate_sitemap(){
$sitemap = new SimpleXMLElement('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>');
foreach($this->sitemap_urls as $url)
{
$url_tag = $sitemap->addChild("url");
$url_tag->addChild("loc", htmlspecialchars($url));
}
return $sitemap->asXML();
}
}
?>
Пример работы с php классом для генерации карты сайты:
<?php
set_time_limit(0);
include("./sitemap.class.php");
$sitemap = new sitemap();
//игнорировать ссылки с расширениями:
$sitemap->set_ignore(array("javascript:", ".css", ".js", ".ico", ".jpg", ".png", ".jpeg", ".swf", ".gif"));
//ссылка Вашего сайта:
$sitemap->get_links("http://diamond-center.com.ua");
//если нужно вернуть просто массив с данными:
//$arr = $sitemap->get_array();
//echo "<pre>";
//print_r($arr);
//echo "</pre>";
header ("content-type: text/xml");
$map = $sitemap->generate_sitemap();
echo $map;
?> |
|
плюнул на все, использовал этот класс, немного модифицировал и все)))
0
|