pinterest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. class pinterest{
  3. public function __construct(){
  4. include "lib/nextpage.php";
  5. $this->nextpage = new nextpage("pinterest");
  6. }
  7. public function getfilters($page){
  8. return [];
  9. }
  10. private function get($url, $get = []){
  11. $curlproc = curl_init();
  12. if($get !== []){
  13. $get = http_build_query($get);
  14. $url .= "?" . $get;
  15. }
  16. curl_setopt($curlproc, CURLOPT_URL, $url);
  17. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  18. curl_setopt($curlproc, CURLOPT_HTTPHEADER,
  19. ["User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:107.0) Gecko/20100101 Firefox/110.0",
  20. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
  21. "Accept-Language: en-US,en;q=0.5",
  22. "Accept-Encoding: gzip",
  23. "DNT: 1",
  24. "Connection: keep-alive",
  25. "Upgrade-Insecure-Requests: 1",
  26. "Sec-Fetch-Dest: document",
  27. "Sec-Fetch-Mode: navigate",
  28. "Sec-Fetch-Site: none",
  29. "Sec-Fetch-User: ?1"]
  30. );
  31. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  32. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  33. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  34. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  35. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  36. $data = curl_exec($curlproc);
  37. if(curl_errno($curlproc)){
  38. throw new Exception(curl_error($curlproc));
  39. }
  40. curl_close($curlproc);
  41. return $data;
  42. }
  43. public function image($get){
  44. $search = $get["s"];
  45. $out = [
  46. "status" => "ok",
  47. "npt" => null,
  48. "image" => []
  49. ];
  50. $filter = [
  51. "source_url" => "/search/pins/?q=" . urlencode($search),
  52. "rs" => "typed",
  53. "data" =>
  54. json_encode(
  55. [
  56. "options" => [
  57. "article" => null,
  58. "applied_filters" => null,
  59. "appliedProductFilters" => "---",
  60. "auto_correction_disabled" => false,
  61. "corpus" => null,
  62. "customized_rerank_type" => null,
  63. "filters" => null,
  64. "query" => $search,
  65. "query_pin_sigs" => null,
  66. "redux_normalize_feed" => true,
  67. "rs" => "typed",
  68. "scope" => "pins", // pins, boards, videos,
  69. "source_id" => null
  70. ],
  71. "context" => []
  72. ]
  73. ),
  74. "_" => substr(str_replace(".", "", (string)microtime(true)), 0, -1)
  75. ];
  76. try{
  77. $json =
  78. json_decode(
  79. $this->get(
  80. "https://www.pinterest.ca/resource/BaseSearchResource/get/",
  81. $filter
  82. ),
  83. true
  84. );
  85. }catch(Exception $error){
  86. throw new Exception("Failed to fetch JSON");
  87. }
  88. if($json === null){
  89. throw new Exception("Failed to decode JSON");
  90. }
  91. //print_r($json);
  92. foreach(
  93. $json
  94. ["resource_response"]
  95. ["data"]
  96. ["results"]
  97. as $item
  98. ){
  99. switch($item["type"]){
  100. case "pin":
  101. /*
  102. Handle image object
  103. */
  104. $images = array_values($item["images"]);
  105. $image = &$images[count($images) - 1]; // original
  106. $thumb = &$images[1]; // 236x
  107. $title = [];
  108. if(
  109. isset($item["grid_title"]) &&
  110. trim($item["grid_title"]) != ""
  111. ){
  112. $title[] = $item["grid_title"];
  113. }
  114. if(
  115. isset($item["description"]) &&
  116. trim($item["description"]) != ""
  117. ){
  118. $title[] = $item["description"];
  119. }
  120. $title = implode(": ", $title);
  121. if(
  122. $title == "" &&
  123. isset($item["board"]["name"]) &&
  124. trim($item["board"]["name"]) != ""
  125. ){
  126. $title = $item["board"]["name"];
  127. }
  128. if($title == ""){
  129. $title = null;
  130. }
  131. $out["image"][] = [
  132. "title" => $title,
  133. "source" => [
  134. [
  135. "url" => $image["url"],
  136. "width" => (int)$image["width"],
  137. "height" => (int)$image["height"]
  138. ],
  139. [
  140. "url" => $thumb["url"],
  141. "width" => (int)$thumb["width"],
  142. "height" => (int)$thumb["height"]
  143. ]
  144. ],
  145. "url" => "https://www.pinterest.com/pin/" . $item["id"]
  146. ];
  147. break;
  148. case "board":
  149. if(isset($item["cover_pin"]["image_url"])){
  150. $image = [
  151. "url" => $item["cover_pin"]["image_url"],
  152. "width" => (int)$item["cover_pin"]["size"][0],
  153. "height" => (int)$item["cover_pin"]["size"][1]
  154. ];
  155. }elseif(isset($item["image_cover_url_hd"])){
  156. /*
  157. $image = [
  158. "url" =>
  159. "width" => null,
  160. "height" => null
  161. ];*/
  162. }
  163. break;
  164. }
  165. }
  166. return $out;
  167. }
  168. private function getfullresimage($image, $has_og){
  169. $has_og = $has_og ? "1200x" : "originals";
  170. return
  171. preg_replace(
  172. '/https:\/\/i\.pinimg\.com\/[^\/]+\//',
  173. "https://i.pinimg.com/" . $has_og . "/",
  174. $image
  175. );
  176. }
  177. }