Created
December 4, 2013 06:04
-
-
Save kijtra/7783087 to your computer and use it in GitHub Desktop.
#PHP PubSubHubbubにURLを送る。引数はURL文字列か、配列で複数URLを渡してもOK。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function send_psh($url) { | |
if(empty($url)){ | |
return false; | |
} | |
$hub = 'http://pubsubhubbub.appspot.com'; | |
//$hub='http://pubsubhubbub.superfeedr.com'; | |
if(is_array($url)) { | |
$mh=curl_multi_init(); | |
$chs = array(); | |
foreach($url as $key => $val) { | |
if (!preg_match('#^https?://#i', $val)) { | |
continue; | |
} | |
$chs[$key] = curl_init(); | |
curl_setopt($chs[$key], CURLOPT_URL, $hub); | |
curl_setopt($chs[$key], CURLOPT_POST, true); | |
curl_setopt($chs[$key], CURLOPT_POSTFIELDS, "hub.mode=publish&hub.url=" . urlencode($val)); | |
curl_multi_add_handle($mh, $chs[$key]); | |
} | |
if(empty($chs)){ | |
return false; | |
} | |
$results = array(); | |
$running = NULL; | |
do { | |
curl_multi_exec($mh, $running); | |
} while($running > 0); | |
foreach($curly as $key => $ch) { | |
if(empty($ch)){ | |
continue; | |
} | |
$results[$key] = (204 == curl_getinfo($ch, CURLINFO_HTTP_CODE)); | |
curl_multi_remove_handle($mh, $ch); | |
} | |
curl_multi_close($mh); | |
return $results; | |
} else { | |
if (!preg_match('#^https?://#i', $url)) { | |
return false; | |
} | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $hub); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, "hub.mode=publish&hub.url=" . urlencode($url)); | |
$res = curl_exec($ch); | |
$result = (204 == curl_getinfo($ch, CURLINFO_HTTP_CODE)); | |
curl_close($ch); | |
return $result; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment