Пример #1
0
<?php

error_reporting(-1);
error_reporting(E_ALL ^ E_NOTICE);
$haystack = "\n<title>The Fast Fix: A judge of few words \n    (The Newsroom)\n</title>\n\n <link>http://us.rd.yahoo.com/dailynews/rss/yahoonewsroom/*http://news.yahoo.com/s/yblog_newsroom/20110221/pl_yblog_newsroom/the-fast-fix-a-judge-of-few-words</link>\n <guid isPermaLink=>yblog_newsroom/20110221/the-fast-fix-a-judge-of-few-words</guid>\n<source>The Newsroom</source>\n<category>politics</category>\n<pubDate>Mon, 21 Feb 2011 03:21:40 GMT</pubDate>\n<description>The Newsroom - Why has Justice Clarence Thomas remained silent for five years? Get The Fix in your e-mail inbox! Click here to sign-up for the Morning Fix newsletter. Click here for the Afternoon Fix newsletter. Follow The Fix on Twitter @thefix or &#8230;</description>\n</item>\n<item>\n<title>The Fast Fix: Where are the candidates? \n    (The Newsroom)\n\n</title>\n <link>http://us.rd.yahoo.com/dailynews/rss/yahoonewsroom/*http://news.yahoo.com/s/yblog_newsroom/20110218/pl_yblog_newsroom/the-fast-fix-where-are-the-candidates</link>\n <guid isPermaLink=>yblog_newsroom/20110218/the-fast-fix-where-are-the-candidates</guid>\n<source>The Newsroom</source>\n<category>politics</category>\n<pubDate>Fri, 18 Feb 2011 14:04:04 GMT</pubDate>\n<description>The Newsroom - When will the first GOP presidential candidate declare, and who will it be? Get The Fix in your e-mail inbox! Click here to sign-up for the Morning Fix newsletter. Click here for the Afternoon Fix newsletter. Follow The Fix on &#8230;</description>\n</item>\n<item>\n\n<title>Love me, love my jeans \n    (The Newsroom)\n</title>\n <link>http://us.rd.yahoo.com/dailynews/rss/yahoonewsroom/*http://news.yahoo.com/s/yblog_newsroom/20110217/od_yblog_newsroom/love-me-love-my-jeans</link>\n <guid isPermaLink=>yblog_newsroom/20110217/love-me-love-my-jeans</guid>\n<source>The Newsroom</source>\n<category>odd</category>\n<pubDate>Thu, 17 Feb 2011 17:44:06 GMT</pubDate>\n<description>The Newsroom - Girlfriends do it all the time - &#039;borrowing&#039; their boyfriend&#039;s T-shirt/sweatshirt/button-down shirt. It&#039;s a romantic gesture, right? (Ex-girlfriends do it too, although they tend not to wear the shirt so much as use it to clean the toilet). But boyfriends &#8230;</description>\n\n</item>\n<item>\n<title>Pay attention: High court isn&#8217;t only for highbrow concepts \n    (The Newsroom)\n</title>\n";
$results = stringExtractor($haystack, "<title>", "</title>");
foreach ($results as $thisResult) {
    echo $thisResult . "<br>";
}
/*-------------------------------------------------------------------------------------------------
stringExtractor
Returns array of strings found between two target strings
-------------------------------------------------------------------------------------------------*/
function stringExtractor($string, $start, $end)
{
    # Setup
    global $cursor;
    global $stringExtractor_results;
    $foundString = -1;
    $string = " " . $string;
    if (!isset($stringExtractor_results)) {
        $stringExtractor_results = array();
    }
    # Extract
    while ($foundString != 0) {
        $ini = strpos($string, $start, $cursor);
        $ini += strlen($start);
        $len = strpos($string, $end, $ini) - $ini;
        $cursor = $ini;
        $result = substr($string, $ini, $len);
        array_push($stringExtractor_results, $result);
Пример #2
0
http://code.google.com/apis/websearch/docs/reference.html
-------------------------------------------------------------------------------------------------*/
# Settings
$keyword = "puppies";
$howManyResults = 8;
# Max is 8
# What page of search results should it pull from?
$randomPage = rand(1, 50);
# Pick a random page. Make the last number smaller if you wan't more relevant results.
# Ping Google
$url = "http://ajax.googleapis.com/ajax/services/search/images?start=" . $randomPage . "&rsz=" . $howManyResults . "&v=1.0&q=" . $keyword;
echo "URL We're fetching from: <a href='{$url}'>{$url}</a><br/><br/>";
# Make cURL call
$curlResults = makeCurlCall($url);
# Parse the results of the cURL call into an array
$images = stringExtractor($curlResults, 'unescapedUrl":"', '","');
# Loop through the array showing all the images
foreach ($images as $thisImage) {
    echo "<img style='width:100px; height:100px' src='{$thisImage}'/><br/>";
}
/*-------------------------------------------------------------------------------------------------
makeCurlCall
-------------------------------------------------------------------------------------------------*/
function makeCurlCall($url)
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $curlResults = curl_exec($ch);
    curl_close($ch);
    return $curlResults;
}
Пример #3
0
<title>cURL example for Yahoo News</title>

<!-- OKTOSHOW --><a href='/classes/viewSource/?path=<?php 
echo $_SERVER['PHP_SELF'];
?>
' target='_blank'>View Source</a><br/><br/><br/>

<?php 
# What URL should we download?
$url = "http://rss.news.yahoo.com/rss/mostviewed";
echo "URL We're fetching from: <a href='{$url}'>{$url}</a><br/><br/>";
# Make curl call
$curlResponse = makeCurlCall($url);
# Parse the results of the curl call, grabbing all the title instances
$titles = stringExtractor($curlResponse, '<title>', '</title>');
# Loop through the titles printing them all out
foreach ($titles as $thisTitle) {
    echo $thisTitle . "<br/>";
}
/*-------------------------------------------------------------------------------------------------
makeCurlCall
-------------------------------------------------------------------------------------------------*/
function makeCurlCall($url)
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $curlResults = curl_exec($ch);
    curl_close($ch);
    return $curlResults;
}
/*-------------------------------------------------------------------------------------------------