Reference documentation and code samples for the Google Developers Knowledge V1 Client class DeveloperKnowledgeClient.
Service Description: The Developer Knowledge API provides programmatic access to Google's public developer documentation, enabling you to integrate this knowledge base into your own applications and workflows.
The API is designed to be the canonical source for machine-readable access to Google's developer documentation.
A typical use case is to first use DeveloperKnowledge.SearchDocumentChunks to find relevant page URIs based on a query, and then use DeveloperKnowledge.GetDocument or DeveloperKnowledge.BatchGetDocuments to fetch the full content of the top results.
All document content is provided in Markdown format.
This class provides the ability to make remote calls to the backing service through method calls that map to API methods.
Many parameters require resource names to be formatted in a particular way. To assist with these names, this class includes a format method for each type of name, and additionally a parseName method to extract the individual identifiers contained within formatted names that are returned by the API.
Namespace
Google \ Developers \ Knowledge \ V1 \ ClientMethods
__construct
Constructor.
| Parameters | |
|---|---|
| Name | Description |
options |
array|Google\ApiCore\Options\ClientOptions
Optional. Options for configuring the service API wrapper. |
↳ apiEndpoint |
string
The address of the API remote host. May optionally include the port, formatted as "
|
↳ credentials |
FetchAuthTokenInterface|CredentialsWrapper
This option should only be used with a pre-constructed Google\Auth\FetchAuthTokenInterface or Google\ApiCore\CredentialsWrapper object. Note that when one of these objects are provided, any settings in $credentialsConfig will be ignored. Important: If you are providing a path to a credentials file, or a decoded credentials file as a PHP array, this usage is now DEPRECATED. Providing an unvalidated credential configuration to Google APIs can compromise the security of your systems and data. It is recommended to create the credentials explicitly |
↳ credentialsConfig |
array
Options used to configure credentials, including auth token caching, for the client. For a full list of supporting configuration options, see Google\ApiCore\CredentialsWrapper::build() . |
↳ disableRetries |
bool
Determines whether or not retries defined by the client configuration should be disabled. Defaults to |
↳ clientConfig |
string|array
Client method configuration, including retry settings. This option can be either a path to a JSON file, or a PHP array containing the decoded JSON data. By default this settings points to the default client config file, which is provided in the resources folder. |
↳ transport |
string|TransportInterface
The transport used for executing network requests. May be either the string |
↳ transportConfig |
array
Configuration options that will be used to construct the transport. Options for each supported transport type should be passed in a key for that transport. For example: $transportConfig = [ 'grpc' => [...], 'rest' => [...], ]; See the Google\ApiCore\Transport\GrpcTransport::build() and Google\ApiCore\Transport\RestTransport::build() methods for the supported options. |
↳ clientCertSource |
callable
A callable which returns the client cert as a string. This can be used to provide a certificate and private key to the transport layer for mTLS. |
↳ logger |
false|LoggerInterface
A PSR-3 compliant logger. If set to false, logging is disabled, ignoring the 'GOOGLE_SDK_PHP_LOGGING' environment flag |
↳ universeDomain |
string
The service domain for the client. Defaults to 'googleapis.com'. |
batchGetDocuments
Retrieves multiple documents, each with its full Markdown content.
The async variant is DeveloperKnowledgeClient::batchGetDocumentsAsync() .
| Parameters | |
|---|---|
| Name | Description |
request |
Google\Developers\Knowledge\V1\BatchGetDocumentsRequest
A request to house fields associated with the call. |
callOptions |
array
Optional. |
↳ retrySettings |
RetrySettings|array
Retry settings to use for this call. Can be a Google\ApiCore\RetrySettings object, or an associative array of retry settings parameters. See the documentation on Google\ApiCore\RetrySettings for example usage. |
| Returns | |
|---|---|
| Type | Description |
Google\Developers\Knowledge\V1\BatchGetDocumentsResponse |
|
use Google\ApiCore\ApiException;
use Google\Developers\Knowledge\V1\BatchGetDocumentsRequest;
use Google\Developers\Knowledge\V1\BatchGetDocumentsResponse;
use Google\Developers\Knowledge\V1\Client\DeveloperKnowledgeClient;
/**
* @param string $formattedNamesElement Specifies the names of the documents to retrieve. A maximum of 20
* documents can be retrieved in a batch. The documents are returned in the
* same order as the `names` in the request.
*
* Format: `documents/{uri_without_scheme}`
* Example: `documents/docs.cloud.google.com/storage/docs/creating-buckets`
* Please see {@see DeveloperKnowledgeClient::documentName()} for help formatting this field.
*/
function batch_get_documents_sample(string $formattedNamesElement): void
{
// Create a client.
$developerKnowledgeClient = new DeveloperKnowledgeClient();
// Prepare the request message.
$formattedNames = [$formattedNamesElement,];
$request = (new BatchGetDocumentsRequest())
->setNames($formattedNames);
// Call the API and handle any network failures.
try {
/** @var BatchGetDocumentsResponse $response */
$response = $developerKnowledgeClient->batchGetDocuments($request);
printf('Response data: %s' . PHP_EOL, $response->serializeToJsonString());
} catch (ApiException $ex) {
printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
}
}
/**
* Helper to execute the sample.
*
* This sample has been automatically generated and should be regarded as a code
* template only. It will require modifications to work:
* - It may require correct/in-range values for request initialization.
* - It may require specifying regional endpoints when creating the service client,
* please see the apiEndpoint client configuration option for more details.
*/
function callSample(): void
{
$formattedNamesElement = DeveloperKnowledgeClient::documentName('[DOCUMENT]');
batch_get_documents_sample($formattedNamesElement);
}
getDocument
Retrieves a single document with its full Markdown content.
The async variant is DeveloperKnowledgeClient::getDocumentAsync() .
| Parameters | |
|---|---|
| Name | Description |
request |
Google\Developers\Knowledge\V1\GetDocumentRequest
A request to house fields associated with the call. |
callOptions |
array
Optional. |
↳ retrySettings |
RetrySettings|array
Retry settings to use for this call. Can be a Google\ApiCore\RetrySettings object, or an associative array of retry settings parameters. See the documentation on Google\ApiCore\RetrySettings for example usage. |
| Returns | |
|---|---|
| Type | Description |
Google\Developers\Knowledge\V1\Document |
|
use Google\ApiCore\ApiException;
use Google\Developers\Knowledge\V1\Client\DeveloperKnowledgeClient;
use Google\Developers\Knowledge\V1\Document;
use Google\Developers\Knowledge\V1\GetDocumentRequest;
/**
* @param string $formattedName Specifies the name of the document to retrieve.
* Format: `documents/{uri_without_scheme}`
* Example: `documents/docs.cloud.google.com/storage/docs/creating-buckets`
* Please see {@see DeveloperKnowledgeClient::documentName()} for help formatting this field.
*/
function get_document_sample(string $formattedName): void
{
// Create a client.
$developerKnowledgeClient = new DeveloperKnowledgeClient();
// Prepare the request message.
$request = (new GetDocumentRequest())
->setName($formattedName);
// Call the API and handle any network failures.
try {
/** @var Document $response */
$response = $developerKnowledgeClient->getDocument($request);
printf('Response data: %s' . PHP_EOL, $response->serializeToJsonString());
} catch (ApiException $ex) {
printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
}
}
/**
* Helper to execute the sample.
*
* This sample has been automatically generated and should be regarded as a code
* template only. It will require modifications to work:
* - It may require correct/in-range values for request initialization.
* - It may require specifying regional endpoints when creating the service client,
* please see the apiEndpoint client configuration option for more details.
*/
function callSample(): void
{
$formattedName = DeveloperKnowledgeClient::documentName('[DOCUMENT]');
get_document_sample($formattedName);
}
searchDocumentChunks
Searches for developer knowledge across Google's developer documentation.
Returns DocumentChunks based on the user's query. There may be many chunks from the same Document. To retrieve full documents, use DeveloperKnowledge.GetDocument or DeveloperKnowledge.BatchGetDocuments with the DocumentChunk.parent returned in the SearchDocumentChunksResponse.results.
The async variant is DeveloperKnowledgeClient::searchDocumentChunksAsync() .
| Parameters | |
|---|---|
| Name | Description |
request |
Google\Developers\Knowledge\V1\SearchDocumentChunksRequest
A request to house fields associated with the call. |
callOptions |
array
Optional. |
↳ retrySettings |
RetrySettings|array
Retry settings to use for this call. Can be a Google\ApiCore\RetrySettings object, or an associative array of retry settings parameters. See the documentation on Google\ApiCore\RetrySettings for example usage. |
| Returns | |
|---|---|
| Type | Description |
Google\ApiCore\PagedListResponse |
|
use Google\ApiCore\ApiException;
use Google\ApiCore\PagedListResponse;
use Google\Developers\Knowledge\V1\Client\DeveloperKnowledgeClient;
use Google\Developers\Knowledge\V1\DocumentChunk;
use Google\Developers\Knowledge\V1\SearchDocumentChunksRequest;
/**
* @param string $query Provides the raw query string provided by the user, such as "How
* to create a Cloud Storage bucket?".
*/
function search_document_chunks_sample(string $query): void
{
// Create a client.
$developerKnowledgeClient = new DeveloperKnowledgeClient();
// Prepare the request message.
$request = (new SearchDocumentChunksRequest())
->setQuery($query);
// Call the API and handle any network failures.
try {
/** @var PagedListResponse $response */
$response = $developerKnowledgeClient->searchDocumentChunks($request);
/** @var DocumentChunk $element */
foreach ($response as $element) {
printf('Element data: %s' . PHP_EOL, $element->serializeToJsonString());
}
} catch (ApiException $ex) {
printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
}
}
/**
* Helper to execute the sample.
*
* This sample has been automatically generated and should be regarded as a code
* template only. It will require modifications to work:
* - It may require correct/in-range values for request initialization.
* - It may require specifying regional endpoints when creating the service client,
* please see the apiEndpoint client configuration option for more details.
*/
function callSample(): void
{
$query = '[QUERY]';
search_document_chunks_sample($query);
}
batchGetDocumentsAsync
| Parameters | |
|---|---|
| Name | Description |
request |
Google\Developers\Knowledge\V1\BatchGetDocumentsRequest
|
optionalArgs |
array
|
| Returns | |
|---|---|
| Type | Description |
GuzzleHttp\Promise\PromiseInterface<Google\Developers\Knowledge\V1\BatchGetDocumentsResponse> |
|
getDocumentAsync
| Parameters | |
|---|---|
| Name | Description |
request |
Google\Developers\Knowledge\V1\GetDocumentRequest
|
optionalArgs |
array
|
| Returns | |
|---|---|
| Type | Description |
GuzzleHttp\Promise\PromiseInterface<Google\Developers\Knowledge\V1\Document> |
|
searchDocumentChunksAsync
| Parameters | |
|---|---|
| Name | Description |
request |
Google\Developers\Knowledge\V1\SearchDocumentChunksRequest
|
optionalArgs |
array
|
| Returns | |
|---|---|
| Type | Description |
GuzzleHttp\Promise\PromiseInterface<Google\ApiCore\PagedListResponse> |
|
static::documentName
Formats a string containing the fully-qualified path to represent a document resource.
| Parameter | |
|---|---|
| Name | Description |
document |
string
|
| Returns | |
|---|---|
| Type | Description |
string |
The formatted document resource. |
static::parseName
Parses a formatted name string and returns an associative array of the components in the name.
The following name formats are supported: Template: Pattern
- document: documents/{document}
The optional $template argument can be supplied to specify a particular pattern, and must match one of the templates listed above. If no $template argument is provided, or if the $template argument does not match one of the templates listed, then parseName will check each of the supported templates, and return the first match.
| Parameters | |
|---|---|
| Name | Description |
formattedName |
string
The formatted name string |
template |
?string
Optional name of template to match |
| Returns | |
|---|---|
| Type | Description |
array |
An associative array from name component IDs to component values. |