api.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. $query = $_GET['q'];
  3. $bookmark = null;
  4. if (array_key_exists("bookmark", $_GET)) {
  5. $bookmark = urldecode($_GET["bookmark"]);
  6. }
  7. $csrftoken = null;
  8. if (array_key_exists("csrftoken", $_GET)) {
  9. $csrftoken = $_GET["csrftoken"];
  10. }
  11. $url = "https://www.pinterest.com/resource/BaseSearchResource/get/";
  12. class SearchResult
  13. {
  14. public $images;
  15. public $bookmark;
  16. }
  17. $header_function = function($ch, $rawheader)
  18. {
  19. global $csrftoken;
  20. $len = strlen($rawheader);
  21. $header = explode(":", $rawheader, 2);
  22. if (count($header) != 2)
  23. return $len;
  24. // we are only interested in set-cookie header
  25. if (trim($header[0]) != "set-cookie")
  26. return $len;
  27. $cookie = explode(";", trim($header[1]), 2);
  28. $cookie = explode("=", $cookie[0], 2);
  29. switch ($cookie[0])
  30. {
  31. case "csrftoken":
  32. $csrftoken = $cookie[1];
  33. }
  34. return $len;
  35. };
  36. $prepare_search_curl_obj = function($query, $bookmark) use ($url, $header_function, $csrftoken)
  37. {
  38. $data_param_obj = array(
  39. "options"=>array(
  40. "query"=>$query
  41. )
  42. );
  43. if ($bookmark != null)
  44. $data_param_obj["options"]["bookmarks"] = array($bookmark);
  45. $data_param = urlencode(json_encode($data_param_obj));
  46. $headers = array();
  47. if ($csrftoken != null)
  48. {
  49. $headers[] = "x-csrftoken: $csrftoken";
  50. $headers[] = "cookie: csrftoken=$csrftoken";
  51. }
  52. $finalurl = $url;
  53. if ($bookmark == null)
  54. $finalurl = "$url?data=$data_param";
  55. $ch = curl_init($finalurl);
  56. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  57. curl_setopt($ch, CURLOPT_HEADERFUNCTION, $header_function);
  58. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  59. if ($bookmark != null)
  60. {
  61. curl_setopt($ch, CURLOPT_POST, true);
  62. curl_setopt($ch, CURLOPT_POSTFIELDS, "data=$data_param");
  63. }
  64. return $ch;
  65. };
  66. $search = function($query, $bookmark) use($prepare_search_curl_obj)
  67. {
  68. $ch = $prepare_search_curl_obj($query, $bookmark);
  69. $response = curl_exec($ch);
  70. $data = json_decode($response);
  71. $images = array();
  72. foreach ($data->{"resource_response"}->{"data"}->{"results"} as $result)
  73. {
  74. $image = $result->{"images"}->{"orig"};
  75. $url = $image->{"url"};
  76. array_push($images, $url);
  77. }
  78. echo json_encode($images);
  79. $result = new SearchResult();
  80. $result->images = $images;
  81. if (property_exists($data->{"resource_response"}, "bookmark"))
  82. $result->bookmark = $data->{"resource_response"}->{"bookmark"};
  83. return $result;
  84. };
  85. $result = $search($query, $bookmark);
  86. if ($result->bookmark != null)
  87. {
  88. $query_encoded = urlencode($query);
  89. $bookmark_encoded = urlencode($result->bookmark);
  90. $csrftoken_encoded = urlencode($csrftoken);
  91. // echo "<h2 style=\"text-align: center;\"><a href=\"/search.php?q=$query_encoded&bookmark=$bookmark_encoded&csrftoken=$csrftoken_encoded\">Next page</a></h2><br><br><br>";
  92. }
  93. header("Content-Type: application/json");
  94. ?>