├── .github
└── FUNDING.yml
├── .gitignore
├── LICENSE
├── README.md
├── composer.json
└── src
└── Bard.php
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | # These are supported funding model platforms
2 |
3 | github: [pj8912]
4 | patreon: # Replace with a single Patreon username
5 | open_collective: # Replace with a single Open Collective username
6 | ko_fi: # Replace with a single Ko-fi username
7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9 | liberapay: # Replace with a single Liberapay username
10 | issuehunt: # Replace with a single IssueHunt username
11 | otechie: # Replace with a single Otechie username
12 | lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
13 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
14 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /vendor/
2 | composer.lock
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2023 John Pinto
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # php-bard-api
2 |
3 |
4 | ***❗This repo is archived❗***
5 |
6 |
7 |
8 |
Use Official Gemini API
9 |
10 | ```
11 | https://ai.google.dev/
12 | ```
13 |
14 |
15 | ----
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 | > A package that returns Response of Google Bard through API
25 |
26 | ## Install
27 | ```
28 | composer require pj8912/php-bard-api
29 | ```
30 |
31 | ## Get Your Keys
32 | - Open [bard.google.com](https://bard.google.com/)
33 | - Open developer tools, click `Application` tab
34 | - In Application under the `Storage` you will find `cookies` dropdown
35 | - Under cookies click on `https://bard.google.com` which will show you all the cookies being used as `Name` and `Value`
36 | - Copy the values the cookies`__Secure-1PSID` and `__Secure-1PSIDTS`
37 |
38 |
39 | ## Run
40 |
41 | > **the value of `__Secure-1PSIDTS` will have to be changed frequently (Google changes it)**
42 |
43 | ```php
44 | require_once 'vendor/autoload.php';
45 | use Pj8912\PhpBardApi\Bard;
46 | //two keys are required which are two cookies values
47 | $_ENV['BARD_API_KEY_X'] = " value of cookie '__Secure-1PSID' ";
48 | $_ENV['BARD_API_KEY_Y'] = " value of cookie '__Secure-1PSIDTS' "
49 | $bard = new Bard();
50 | $input_text = "Hello, Bard!"; // Input text for the conversation
51 | $result = $bard->get_answer($input_text); // Get the response from Bard
52 |
53 | // bard reply
54 | print($result['choices'][0]['content'][0]);
55 |
56 | ```
57 | ## License
58 | This project is licensed under the [MIT](https://opensource.org/license/mit/) License
59 |
60 | ## Reference
61 | - [dsdanielpark/Bard-API](https://github.com/dsdanielpark/Bard-API)
62 |
63 | ### Important Notice
64 | The user assumes all legal responsibilities associated with using the BardAPI package. This PHP package merely facilitates easy access to Google Bard for developers. Users are solely responsible for managing data and using the package appropriately. For further information, please consult the Google Bard Official Document.
65 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "pj8912/php-bard-api",
3 | "description": "A PHP package for interacting with the Bard API",
4 | "type": "library",
5 | "license": "MIT",
6 | "minimum-stability": "dev",
7 | "autoload": {
8 | "psr-4": {
9 | "Pj8912\\PhpBardApi\\": "src/"
10 | }
11 | },
12 | "authors": [
13 | {
14 | "name": "pj8912",
15 | "email": "jpintofun2b@gmail.com"
16 | }
17 | ],
18 | "require": {
19 | "php": "^8.1"
20 | },
21 | "prefer-stable": true
22 | }
23 |
--------------------------------------------------------------------------------
/src/Bard.php:
--------------------------------------------------------------------------------
1 | proxies = $proxies;
18 | $this->timeout = $timeout;
19 | $headers = [
20 | "Host: bard.google.com",
21 | "X-Same-Domain: 1",
22 | "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
23 | "Content-Type: application/x-www-form-urlencoded;charset=UTF-8",
24 | "Origin: https://bard.google.com",
25 | "Referer: https://bard.google.com/",
26 | ];
27 | $this->reqid = (int) rand(pow(10, 3-1), pow(10, 3)-1);
28 | $this->conversation_id = "";
29 | $this->response_id = "";
30 | $this->choice_id = "";
31 |
32 | if ($session === null) {
33 | $this->session = curl_init();
34 | curl_setopt($this->session, CURLOPT_HTTPHEADER, $headers);
35 | curl_setopt($this->session, CURLOPT_COOKIE, "__Secure-1PSID=" . $_ENV["BARD_API_KEY_X"]."; __Secure-1PSIDTS=" . $_ENV["BARD_API_KEY_Y"]);
36 | curl_setopt($this->session, CURLOPT_RETURNTRANSFER, true);
37 | } else {
38 | $this->session = $session;
39 | }
40 |
41 | $this->SNlM0e = $this->_get_snim0e();
42 | }
43 |
44 | private function _get_snim0e() {
45 | curl_setopt($this->session, CURLOPT_URL, "https://bard.google.com/");
46 | curl_setopt($this->session, CURLOPT_TIMEOUT, $this->timeout);
47 | curl_setopt($this->session, CURLOPT_PROXY, $this->proxies);
48 | curl_setopt($this->session, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
49 | curl_setopt($this->session, CURLOPT_FOLLOWLOCATION, true);
50 | curl_setopt($this->session, CURLOPT_SSL_VERIFYHOST, false);
51 | curl_setopt($this->session, CURLOPT_SSL_VERIFYPEER, false);
52 | curl_setopt($this->session, CURLOPT_RETURNTRANSFER, true);
53 | $resp = curl_exec($this->session);
54 | if (curl_getinfo($this->session, CURLINFO_HTTP_CODE) !== 200) {
55 | throw new \Exception("Response Status: " . curl_getinfo($this->session, CURLINFO_HTTP_CODE));
56 | }
57 | preg_match('/"SNlM0e":"(.*?)"/', $resp, $matches);
58 | preg_match('/"cfb2h":"(.*?)"/', $resp, $matchesCfb2h);
59 | $this->cfb2h = $matchesCfb2h[1];
60 | return $matches[1];
61 | }
62 |
63 | public function get_answer($input_text) {
64 | $params = [
65 | "bl" => $this->cfb2h,
66 | "_reqid" => (string) $this->reqid,
67 | "rt" => "c",
68 | ];
69 | $input_text_struct = [
70 | [$input_text],
71 | null,
72 | [$this->conversation_id, $this->response_id, $this->choice_id],
73 | ];
74 | $data = [
75 | "f.req" => json_encode([null, json_encode($input_text_struct)]),
76 | "at" => $this->SNlM0e,
77 | ];
78 | curl_setopt($this->session, CURLOPT_URL, "https://bard.google.com/_/BardChatUi/data/assistant.lamda.BardFrontendService/StreamGenerate?" . http_build_query($params));
79 | curl_setopt($this->session, CURLOPT_POST, true);
80 | curl_setopt($this->session, CURLOPT_POSTFIELDS, http_build_query($data));
81 | curl_setopt($this->session, CURLOPT_TIMEOUT, $this->timeout);
82 | curl_setopt($this->session, CURLOPT_PROXY, $this->proxies);
83 | curl_setopt($this->session, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
84 | curl_setopt($this->session, CURLOPT_FOLLOWLOCATION, true);
85 | curl_setopt($this->session, CURLOPT_SSL_VERIFYHOST, false);
86 | curl_setopt($this->session, CURLOPT_SSL_VERIFYPEER, false);
87 | curl_setopt($this->session, CURLOPT_RETURNTRANSFER, true);
88 | $resp = curl_exec($this->session);
89 | $resp_dict = json_decode(explode("\n", $resp)[3], true)[0][2];
90 | if ($resp_dict === null) {
91 | return ["content" => "Response Error: " . $resp . "."];
92 | }
93 | $parsed_answer = json_decode($resp_dict, true);
94 | $bard_answer = [
95 | "content" => $parsed_answer[0][0],
96 | "conversation_id" => $parsed_answer[1][0],
97 | "response_id" => $parsed_answer[1][1],
98 | "factualityQueries" => $parsed_answer[3],
99 | "textQuery" => $parsed_answer[2][0] ?? "",
100 | "choices" => array_map(function ($i) {
101 | return ["id" => $i[0], "content" => $i[1]];
102 | }, $parsed_answer[4]),
103 | ];
104 | $this->conversation_id = $bard_answer["conversation_id"];
105 | $this->response_id = $bard_answer["response_id"];
106 | $this->choice_id = $bard_answer["choices"][0]["id"];
107 | $this->reqid += 100000;
108 |
109 | return $bard_answer;
110 | }
111 | }
112 |
--------------------------------------------------------------------------------