├── ProductHunt.php └── README.md /ProductHunt.php: -------------------------------------------------------------------------------- 1 | 9 | */ 10 | class ProductHunt 11 | { 12 | 13 | protected $_hostDomain; 14 | protected $_client_id; 15 | protected $_client_secret; 16 | protected $_client_access_token; 17 | 18 | public function __construct($client_id,$client_secret){ 19 | $this->_hostDomain = "https://api.producthunt.com"; 20 | $this->_client_id = $client_id; 21 | $this->_client_secret = $client_secret; 22 | 23 | $this->getClientLevelToken(); 24 | } 25 | 26 | /** 27 | * Retrieve Client Level Access Token 28 | */ 29 | public function getClientLevelToken(){ 30 | $endpoint = "/v1/oauth/token"; 31 | 32 | $params = array( 33 | 'client_id' => $this->_client_id, 34 | 'client_secret' => $this->_client_secret, 35 | 'grant_type' => 'client_credentials' 36 | ); 37 | $response = $this->post($endpoint,$params); 38 | 39 | $this->_client_access_token = $response->access_token; 40 | 41 | return $response; 42 | } 43 | 44 | /** 45 | * Retrieve posts for a given day 46 | */ 47 | public function getPostsByDay($date='today'){ 48 | $endpoint = "/v1/posts"; 49 | $params['day'] = date('Y-m-d',strtotime($date)); 50 | return $this->get($endpoint,$params); 51 | } 52 | 53 | /** 54 | * Retrieve posts for a given day 55 | * @array $params [search,older,newer,per_page] 56 | */ 57 | public function getNewestPosts($params=array()){ 58 | $endpoint = "/v1/posts/all"; 59 | return $this->get($endpoint,$params); 60 | } 61 | 62 | /** 63 | * Retrieve votes for a given post 64 | * @array $params [older,newer,per_page,order] 65 | * @int $id Id of Post 66 | */ 67 | public function getPostVotes($id,$params=array()){ 68 | $endpoint = "/v1/posts/$id/votes"; 69 | return $this->get($endpoint,$params); 70 | } 71 | 72 | 73 | /** 74 | * Retrieve post comments 75 | * @array $params [older,newer,per_page,order] 76 | * @int $id Id of User 77 | */ 78 | public function getPostComments($id,$params=array()){ 79 | $endpoint = "/v1/posts/$id/comments"; 80 | return $this->get($endpoint,$params); 81 | } 82 | 83 | /** 84 | * Show details for a single post 85 | * @int $id Id of a single post 86 | */ 87 | public function showPost($id){ 88 | $endpoint = "/v1/posts/1"; 89 | $params = array(); 90 | return $this->get($endpoint,$params); 91 | } 92 | 93 | /** 94 | * Retrieve votes for a given user 95 | * @array $params [older,newer,per_page,order] 96 | * @int $id Id of User 97 | */ 98 | public function getUserVotes($id,$params=array()){ 99 | $endpoint = "/v1/users/$id/votes"; 100 | return $this->get($endpoint,$params); 101 | } 102 | 103 | 104 | /** 105 | * Retrieve post comments 106 | * @array $params [older,newer,per_page,order] 107 | * @int $id Id of User 108 | */ 109 | public function getUserComments($id,$params=array()){ 110 | $endpoint = "/v1/users/$id/comments"; 111 | return $this->get($endpoint,$params); 112 | } 113 | 114 | 115 | /** 116 | * Retrieve user records 117 | * @array $params [older,newer,per_page,order] 118 | */ 119 | public function getUsers($params=array()){ 120 | $endpoint = "/v1/users"; 121 | return $this->get($endpoint,$params); 122 | } 123 | 124 | /** 125 | * Retrieve user records 126 | * @array $params [older,newer,per_page,order] 127 | */ 128 | public function getUser($username){ 129 | $endpoint = "/v1/users/".$username; 130 | return $this->get($endpoint,$params); 131 | } 132 | 133 | /** 134 | * Send POST request 135 | */ 136 | public function post($endpoint,$params=array()){ 137 | $params['access_token'] = $this->_client_access_token; 138 | $ch = curl_init(); 139 | 140 | curl_setopt($ch, CURLOPT_URL,$this->_hostDomain.$endpoint); 141 | curl_setopt($ch, CURLOPT_POST, 1); 142 | 143 | curl_setopt($ch, CURLOPT_POSTFIELDS, 144 | http_build_query($params)); 145 | 146 | // receive server response 147 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 148 | 149 | $server_output = curl_exec ($ch); 150 | 151 | curl_close ($ch); 152 | 153 | return json_decode($server_output); 154 | } 155 | 156 | /** 157 | * Send GET request 158 | */ 159 | public function get($endpoint,$params=array()){ 160 | $params['access_token'] = $this->_client_access_token; 161 | $ch = curl_init(); 162 | 163 | $query = http_build_query($params); 164 | 165 | curl_setopt($ch,CURLOPT_URL,$this->_hostDomain.$endpoint.'?'.$query); 166 | curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); 167 | 168 | $output=curl_exec($ch); 169 | 170 | curl_close($ch); 171 | return json_decode($output); 172 | } 173 | 174 | } 175 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Product Hunt API PHP Library 2 | ============================== 3 | 4 | This is a PHP Library for interacting with the Product Hunt API. I developed it during the 2014 YC/ProductHunt hackathon to use for http://HunterList.co 5 | 6 | Api Docs available at: https://api.producthunt.com/v1/docs/ 7 | 8 | How to use 9 | --------------- 10 | 11 | require_once('ProductHunt.php'); 12 | 13 | //init object 14 | $productHunt = new ProductHunt($client_id,$client_secret); 15 | 16 | //get posts for a given day 17 | $posts = $productHunt->getPostsByDay('today'); 18 | 19 | //get newest posts 20 | $newestPosts = $productHunt->getNewestPosts(array('per_page'=>10)); 21 | 22 | //get votes for a post 23 | $postVotes = $productHunt->getPostVotes($post_id,array('order'=>'asc')); 24 | 25 | //get comments for a post 26 | $postComments = $productHunt->getPostComments($post_id,array('order'=>'asc')); 27 | 28 | //get details for particular post 29 | $postDetails = $productHunt->showPost($post_id); 30 | 31 | //get votes for a user 32 | $userVotes = $productHunt->getUserVotes($user_id,array('per_page'=>10)); 33 | 34 | //get comments for a user 35 | $userComments = $productHunt->getUserComments($user_id,array('order'=>'desc')); 36 | 37 | //get a list of users 38 | $allUsers = $productHunt->getUsers(array('per_page'=>50)); 39 | 40 | //get info about a specific user 41 | $userData = $productHunt->getUser($username); 42 | 43 | 44 | 45 | --------------------------------------------------------------------------------