Jump to content

How To Solve This Php Rss Parse Problem?


profmustamar

Recommended Posts

Hello, I am new here.

 

I am using WordPress, and I want to show 5 items from Search Engine (bing), related to my blog content.

I want to change "This+Is+Search+Keyword" with my post title ($title or the_title()), so every single post in my blog will have 5 related bing results.

 

Here the code I am using. Can somebody help me?

 

<?php
function DisplayTitle($title){
$title = the_title();			   //this is my post title//
$title = str_replace(' ', '+', $title);
return ($title);
}
$FixTitle = DisplayTitle($title);

$rss = new DOMDocument();
$rss->load('http://www.bing.com/search?q=This+Is+Search+Keyword&format=RSS');	  // <<<<<<   I want to change "this is keyword" with "my post title"//

$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
 'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
 'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
 'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
 'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
 );
array_push($feed, $item);
}
$limit = 5;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
}
?>

Link to comment
https://forums.phpfreaks.com/topic/269677-how-to-solve-this-php-rss-parse-problem/
Share on other sites

I got error when Echo it first.

 

 

btw, Please check in my blog: http://caracepathamils.com/cara-mendapatkan-anak-perempuan/

You will see under the comment form, "Related post to... (post title)".

There should be the results from this code.

 

 


<h2>Related Posts to <u><?php the_title();?></u>:</h2>
<?php
function DisplayTitle($posttitle){
$posttitle = the_title();					  //this is my post title//
$posttitle = str_replace(' ', '+', $posttitle);
return ($posttitle);
}
$FixTitle = DisplayTitle($posttitle);

$rss = new DOMDocument();
$rss->load('http://www.bing.com/search?q=This+Is+Keyword&format=RSS');  // <<<  I want to change "this is keyword" with "my post title"//

$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
 'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
 'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
 'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
 'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
 );
array_push($feed, $item);
}
$limit = 5;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
}
?>



Okay. :)

After changing to this:

$rss->load('http://www.bing.com/search?q='.$FixTitle.'&format=RSS');

I got no result. (Check attachment)

 

Then after I echo it first, I got this:

 

Parse error: syntax error, unexpected T_ECHO in /home/mustamar/public_html/caracepathamils.com/wp-content/themes/twentyten/single.php on line 33

post-132872-0-62971700-1350659230_thumb.jpg

Then your syntax is wrong. You didn't post your new code. We can't see your screen. We can't see your code. We can't see your errors. You have a basic syntax error, look at the code you added. Are you using an IDE with syntax highlighting? It would be obvious.

I change to this:


$rss->load('http://www.bing.com/search?q='.echo $FixTitle.'&format=RSS');

 

I got syntax error, unexpected T_ECHO.

 

<h2>Related Posts to <u><?php the_title();?></u>:</h2>

<?php
function DisplayTitle($posttitle){
$posttitle = the_title();	   /** this is my post title */
$posttitle = str_replace(' ', '+', $posttitle);
return ($posttitle);
}

$FixTitle = DisplayTitle($posttitle);

$rss = new DOMDocument();

$rss->load('http://www.bing.com/search?q='.echo $FixTitle.'&format=RSS');  /* <<<  I want to change "this is keyword" with "my post title" */

$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
 'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
 'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
 'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
 'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
 );
array_push($feed, $item);
}
$limit = 5;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
}
?>

I found the solution. Thank you, anyway.

 


<h2>Related Posts to <u><?php the_title();?></u>:</h2>

<?php
function DisplayTitle($posttitle){
$posttitle = the_title('','',false);	   /** this is my post title */
$posttitle = str_replace(' ', '+', $posttitle);
return ($posttitle);
}


$FixTitle = DisplayTitle($posttitle);

$rss = new DOMDocument();

$rss->load("http://www.bing.com/search?q=".$FixTitle."&format=RSS");  /* <<<  I want to change "this is keyword" with "my post title" */

$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
 'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
 'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
 'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
 'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
 );
array_push($feed, $item);
}
$limit = 5;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.