Developers > Code library for PHP

Code library for PHP

Download and installation

This library is released under the BSD license.

Requirements: PHP 5.0, curl extension. If you need OAuth authentication, then you also need to install PECL/OAuth.

Installation: simply copy the *.inc files to your PHP source code directory. Include just this file:


require_once('likeorhate.inc');

Tutorial

There are many files in the test/ subdirectory, which can be used as a tutorial. They are very simple and well documented. We'll run through test/thing.php as an example.

  1. <?php
  2. /*
  3.  * likeorhate.com
  4.  * Copyright (c) 2008-2010 likeorhate.com
  5.  *
  6.  * Released under BSD license.
  7.  */
  8.  
  9. /*
  10.  * This file shows how to get data about things.
  11.  */
  12.  
  13. /*
  14.  * Generates a random key. Used to get a non-existing thing.
  15.  */
  16. function randomKey($length = 32) {
  17. $pattern = "1234567890abcdefghijklmnopqrstuvwxyz";
  18. $key = "";
  19. for($i = 0; $i < $length; $i++) {
  20. $key .= $pattern{rand(0,35)};
  21. }
  22. return $key;
  23. }
  24.  
  25.  
  26. // Base file. Include only this file, the others are included automatically.
  27. require_once('../likeorhate/likeorhate.inc');
  28.  
  29. // Get the base singleton, which is used to setup the requests.
  30. $loh = LikeOrHate::singleton();
  31.  
  32. // Use the sandbox server. We can mess it as much as we want, so it's great for
  33. // debugging. Use "production" when your application is deployed.
  34. $loh->setServer('sandbox');
  35.  
  36. // construct the thing. Here we are using a string (its label), so we set id=0.
  37. $likeorhate = new LoHThing(0, 'likeorhate');
  38.  
  39. // get basic information
  40. $base = $likeorhate->getBase();
  41. if ($base['status']) {
  42. echo 'Base: ';
  43. var_dump($base);
  44. }
  45. else {
  46. echo "Error getting base.\n";
  47. }
  48.  
  49. // get extended information
  50. $information = $likeorhate->getInformation();
  51. if ($information['status']) {
  52. echo 'information: ';
  53. var_dump($information);
  54. }
  55. else {
  56. echo "Error getting information.\n";
  57. }
  58.  
  59. // get up to 5 comments, starting at 0,
  60. $comments = $likeorhate->getComments(0, 5);
  61. if ($comments['status']) {
  62. echo 'comments: ';
  63. var_dump($comments);
  64. }
  65. else {
  66. var_dump($comments);
  67. echo "Error getting comments.\n";
  68. }
  69.  
  70. // get trackback information
  71. $trackback = $likeorhate->getTrackback(0, 5);
  72. if ($trackback['status']) {
  73. echo 'trackback: ';
  74. var_dump($trackback);
  75. }
  76. else {
  77. echo "Error getting trackback.\n";
  78. }
  79.  
  80. $notexist = new LoHThing(randomKey(32)); // this should not exist
  81. $base2 = $notexist->getBase();
  82. // we expect $base2['data'] == null (does not exist)
  83. var_dump($base2);
  84.  
  85. ?>

OAuth

Here's a small tutorial on how to use 3-legged OAuth. This is what you need if you want to access LikeOrHate on behalf of other users securely. In this example we will create a new thing on behalf of another user. Let's start by noting that we have three entities here:

  1. LikeOrHate.com.
  2. Your application or website.
  3. The user.

Rest of tutorial to come.


Back to main developers page

 
All Content in this site is the sole responsibility of the person from whom such Content originated. See our Terms of service