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
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>';
}
?>



Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>';
}
?>

Link to comment
Share on other sites

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>';
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.