Jump to content

PHP and Twitter


ksduded

Recommended Posts

I am trying to place a twitter feed on my php page. I am following this tutorial: http://frankkoehl.com/2008/11/display-twitter-updates-on-your-website/

 

It uses the cURL functions. I have php 5+ so I am assuming its built in. However I get the error:

Fatal error: Call to undefined function twitter_status()

 

The programmer of the code suggested to ask other php'ers, so I present my case to you.

 

I have put the fuction twitter_status(etc.....

inside the <head> tags within <script type='text/javascript'>

This is my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type='text/javascript'>
function twitter_status($twitter_id, $hyperlinks = true) {
  $c = curl_init();
  curl_setopt($c, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/$twitter_id.xml");
  curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 3);
  curl_setopt($c, CURLOPT_TIMEOUT, 5);
  $response = curl_exec($c);
  $responseInfo = curl_getinfo($c);
  curl_close($c);
  if (intval($responseInfo['http_code']) == 200) {
    if (class_exists('SimpleXMLElement')) {
      $xml = new SimpleXMLElement($response);
      return $xml;
    } else {
      return $response;
    }
  } else {
    return false;
  }
}
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</script>
<title>Untitled Document</title>
</head>

<ul>
<?php

if ($twitter_xml = twitter_status('61289623')) {
  foreach ($twitter_xml->status as $key => $status) {
?>
  <li><?php echo $status->text; ?></li>
<?php
    ++$i;
    if ($i == 1) break;
  }
?>
  <li><a href="http://twitter.com/YOUR_PROFILE_HERE">more...</a></li>
<?php
} else {
  echo 'Sorry, Twitter seems to be unavailable at the moment...again...';
}
?>
</ul>
<body>
</body>
</html>

 

any help will be greatly appreciated

Link to comment
https://forums.phpfreaks.com/topic/168289-php-and-twitter/
Share on other sites

Try this instead:

<?php
function twitter_status($twitter_id, $hyperlinks = true) {
  $c = curl_init();
  curl_setopt($c, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/$twitter_id.xml");
  curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 3);
  curl_setopt($c, CURLOPT_TIMEOUT, 5);
  $response = curl_exec($c);
  $responseInfo = curl_getinfo($c);
  curl_close($c);
  if (intval($responseInfo['http_code']) == 200) {
    if (class_exists('SimpleXMLElement')) {
      $xml = new SimpleXMLElement($response);
      return $xml;
    } else {
      return $response;
    }
  } else {
    return false;
  }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<ul>
<?php

if ($twitter_xml = twitter_status('61289623')) {
  foreach ($twitter_xml->status as $key => $status) {
?>
  <li><?php echo $status->text; ?></li>
<?php
    ++$i;
    if ($i == 1) break;
  }
?>
  <li><a href="http://twitter.com/YOUR_PROFILE_HERE">more...</a></li>
<?php
} else {
  echo 'Sorry, Twitter seems to be unavailable at the moment...again...';
}
?>
</ul>
<body>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/168289-php-and-twitter/#findComment-887652
Share on other sites

Try this instead:

<?php
function twitter_status($twitter_id, $hyperlinks = true) {
  $c = curl_init();
  curl_setopt($c, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/$twitter_id.xml");
  curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 3);
  curl_setopt($c, CURLOPT_TIMEOUT, 5);
  $response = curl_exec($c);
  $responseInfo = curl_getinfo($c);
  curl_close($c);
  if (intval($responseInfo['http_code']) == 200) {
    if (class_exists('SimpleXMLElement')) {
      $xml = new SimpleXMLElement($response);
      return $xml;
    } else {
      return $response;
    }
  } else {
    return false;
  }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<ul>
<?php

if ($twitter_xml = twitter_status('61289623')) {
  foreach ($twitter_xml->status as $key => $status) {
?>
  <li><?php echo $status->text; ?></li>
<?php
    ++$i;
    if ($i == 1) break;
  }
?>
  <li><a href="http://twitter.com/YOUR_PROFILE_HERE">more...</a></li>
<?php
} else {
  echo 'Sorry, Twitter seems to be unavailable at the moment...again...';
}
?>
</ul>
<body>
</body>
</html>

 

I tried that and got this error instead:

Fatal error: Call to undefined function curl_init()

 

I am guessing that my php does not include the cURL library? It is php 5+ so I assumed that it should be included

Link to comment
https://forums.phpfreaks.com/topic/168289-php-and-twitter/#findComment-887654
Share on other sites

I am guessing that my php does not include the cURL library? It is php 5+ so I assumed that it should be included

 

Your guess is correct. Are you using free hosting? I've known it be disabled on those kind of hosts before.

i am using a dedicated server with iweb.com

 

and I can't find the php.ini .... there is a comment that i need to remove to make cURL work....

 

WHM is too confusing

Link to comment
https://forums.phpfreaks.com/topic/168289-php-and-twitter/#findComment-887685
Share on other sites

I decided to look at Twitters API to see how easy it is to grab stuff. Here's a script I built a couple minutes ago to help you. 

 

<?php
function getTweets($user){
$count = 0;
$xml = simplexml_load_file("http://search.twitter.com/search.atom?q=from:".$user);
foreach ($xml->entry as $val){
	$output[$count]['published'] = (string)$val->published;
	$output[$count]['direct_link'] = (string)$val->link->attributes()->href;
	$output[$count]['message'] = (string)$val->title;
	$count++;
}
return $output;
}
$array = getTweets("jonsjava");
print_r($array);
?>

change the user to your user. (where it says getTweets("jonsjava") change jonsjava)

Link to comment
https://forums.phpfreaks.com/topic/168289-php-and-twitter/#findComment-887790
Share on other sites

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.