[ 'header' => 'User-Agent: Mozilla/5.0' . PHP_EOL . 'Cookie: PHPSESSID=' . SESSION_ID, 'ignore_errors' => true ] ]; if (HTTP_PROXY) { $stream_context['http']['proxy'] = 'tcp://' . HTTP_PROXY; } stream_context_set_default($stream_context); function get_lang_query() { if (function_exists('get_string')) { return 'lang=' . get_string('pixiv-lang'); } return 'lang=en'; } function get_json($url, $associative = false) { $response = file_get_contents($url); if (!isset($http_response_header)) { return [false, 'network-error']; } $response_code = substr($http_response_header[0], 9, 3); if ($response_code == '401') { return [false, 'unauthorized']; } return [json_decode($response, $associative), false]; } function get_discovery() { return get_json(sprintf( 'https://www.pixiv.net/ajax/discovery/artworks?mode=%s&limit=%s&%s', get_closest_mode(), 100, get_lang_query() )); } function get_artwork_info($id) { list($response, $error) = get_json(sprintf( 'https://www.pixiv.net/ajax/illust/%s?%s', $id, get_lang_query() )); if ($error) return [false, $error]; if (empty($response->body)) return [false, 'artwork.not-found']; return [$response, false]; } function get_artwork_pages($id) { list($response, $error) = get_json(sprintf( 'https://www.pixiv.net/ajax/illust/%s/pages', $id ), true); if ($error) return [false, $error]; if (empty($response['body'])) return [false, 'artwork.not-found']; return [$response, false]; } function get_artwork_comments($id, $offset, $limit) { list($response, $error) = get_json(sprintf( 'https://www.pixiv.net/ajax/illusts/comments/roots?illust_id=%s&offset=%s&limit=%s', $id, $offset, $limit )); if ($error) return [false, $error]; if (empty($response->body)) return [false, 'artwork.not-found']; if (empty($response->body->comments)) return [false, 'artwork.no-comments']; return [$response, false]; } function get_artwork_comment_replies($id, $page) { list($response, $error) = get_json(sprintf( 'https://www.pixiv.net/ajax/illusts/comments/replies?comment_id=%s&page=%s', $id, $page )); if ($error) return [false, $error]; if (empty($response->body->comments)) return [false, 'artwork.no-replies']; return [$response, false]; } function get_artwork_related($id) { return get_json(sprintf( 'https://www.pixiv.net/ajax/illust/%s/recommend/init?limit=%s&%s', $id, 120, get_lang_query() )); } function get_search($query, $page) { return get_json(sprintf( 'https://www.pixiv.net/ajax/search/artworks/%s?mode=%s&p=%s&%s', urlencode($query), get_closest_mode(), $page, get_lang_query() )); } function get_tag_info($query) { return get_json(sprintf( 'https://www.pixiv.net/ajax/search/tags/%s', urlencode($query) )); } function get_user_info($id) { list($response, $error) = get_json(sprintf( 'https://www.pixiv.net/ajax/user/%s?full=1', $id )); if ($error) return [false, $error]; if (empty($response->body)) return [false, 'user.not-found']; return [$response, false]; } function get_user_bookmarks($id, $offset, $limit) { list($response, $error) = get_json(sprintf( 'https://www.pixiv.net/ajax/user/%s/illusts/bookmarks?tag=&offset=%s&limit=%s&rest=show&%s', $id, $offset, $limit, get_lang_query() )); if ($error) return [false, $error]; if (empty($response->body)) return [false, 'user.not-found']; return [$response, false]; } function get_user_artworks_with_tag($id, $category, $tag, $offset, $limit) { $request_path = [ 'artworks' => 'illustmanga', 'illustrations' => 'illusts', 'manga' => 'manga' ][$category]; list($response, $error) = get_json(sprintf( 'https://www.pixiv.net/ajax/user/%s/%s/tag?tag=%s&offset=%s&limit=%s&%s', $id, $request_path, urlencode($tag), $offset, $limit, get_lang_query() )); if ($error) return [false, $error]; if (empty($response->body)) return [false, 'user.not-found']; return [$response, false]; } function get_user_artworks_list($id) { list($response, $error) = get_json(sprintf( 'https://www.pixiv.net/ajax/user/%s/profile/all', $id ), true); if ($error) return [false, $error]; if (empty($response['body'])) return [false, 'user.not-found']; return [$response, false]; } function get_user_artworks($user_id, $artwork_ids) { list($response, $error) = get_json(sprintf( 'https://www.pixiv.net/ajax/user/%s/profile/illusts?work_category=illustManga&is_first_page=0&%s&%s', $user_id, get_lang_query(), http_build_query(['ids' => $artwork_ids]) )); if ($error) return [false, $error]; if (empty($response->body)) return [false, 'user.not-found']; return [$response, false]; } function get_frequent_tags($artwork_ids) { return get_json(sprintf( 'https://www.pixiv.net/ajax/tags/frequent/illust?%s&%s', get_lang_query(), http_build_query(['ids' => $artwork_ids]) )); }