1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- function prepareSearchCurlObj($query, $bookmark = null, $url, $csrftoken = null, $header_function) {
- $data_param_obj = [
- "options" => [
- "query" => $query,
- "bookmarks" => $bookmark ? [$bookmark] : null
- ]
- ];
- $data_param = urlencode(json_encode(array_filter($data_param_obj['options'])));
- $headers = [];
- if ($csrftoken) {
- $headers[] = "x-csrftoken: $csrftoken";
- $headers[] = "cookie: csrftoken=$csrftoken";
- }
- $finalurl = $bookmark ? $url : "$url?data=$data_param";
- $ch = curl_init($finalurl);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HEADERFUNCTION, $header_function);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- if ($bookmark) {
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, "data=$data_param");
- }
- return $ch;
- }
- function search($query, $bookmark, $url, $csrftoken, $header_function) {
- $ch = prepareSearchCurlObj($query, $bookmark, $url, $csrftoken, $header_function);
-
- $response = curl_exec($ch);
-
- if ($response === false) {
-
- $error = curl_error($ch);
- curl_close($ch);
- return json_encode(['error' => 'CURL Error: ' . $error]);
- }
- curl_close($ch);
- $data = json_decode($response);
- $images = [];
- if (isset($data->resource_response->data->results)) {
- foreach ($data->resource_response->data->results as $result) {
- $url = $result->images->orig->url ?? null;
- if ($url) {
- $images[] = $url;
- }
- }
- }
- $result = new SearchResult();
- $result->images = $images;
- $result->bookmark = $data->resource_response->bookmark ?? null;
- return $result;
- }
- header("Content-Type: application/json");
- $result = search($query, $bookmark, $url, $csrftoken, $header_function);
- if ($result->bookmark) {
- $query_encoded = urlencode($query);
- $bookmark_encoded = urlencode($result->bookmark);
- $csrftoken_encoded = urlencode($csrftoken);
-
-
- }
- echo json_encode($result);
- ?>
|