giphy (1).php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. $choice = 'clear';
  3. $url = 'http://api.giphy.com/v1/gifs/random?api_key=key&tag='.$choice;
  4. // Get the document at this URL
  5. $response = file_get_contents($url);
  6. // Print the raw response for debugging
  7. print_r($response);
  8. include 'db.php';
  9. $obj = new dataobject($_SERVER['REMOTE_ADDR']);
  10. // Decode the response into an object (this is the default behavior of json_decode)
  11. $response = json_decode($response);
  12. // Check if the response contains 'data' and it's an object (not an array)
  13. if (isset($response->data) && is_object($response->data)) {
  14. // Access the first element of 'data' object directly
  15. $gif = $response->data; // Since 'data' is an object, there's no need to index it like an array
  16. // Ensure 'images' and 'original' are set before accessing them
  17. if (isset($gif->images->original->url)) {
  18. $imageUrl = $gif->images->original->url;
  19. // Set timezone and format date
  20. date_default_timezone_set('America/New_York');
  21. $date_time = strftime('%Y-%m-%d %H:%M:%S', time());
  22. // Connect to the database
  23. $obj->_setname("name");
  24. $obj->_setpass("pass");
  25. $obj->_setconn("conn");
  26. $obj->connect("connect");
  27. // Prepare the SQL query to insert the GIF data into the database
  28. $insertsql = "INSERT INTO localhost (ident, url, date) VALUES (";
  29. $insertsql .= "'".$gif->id."', '".$imageUrl."', '".$date_time."')";
  30. // Run the query
  31. $obj->run($insertsql);
  32. // Optionally print the SQL query for debugging
  33. // print_r($insertsql);
  34. } else {
  35. echo "Error: Missing 'images' or 'original' in the response.";
  36. }
  37. } else {
  38. echo "Error: No valid GIF data found in the response.";
  39. }
  40. ?>