Is there an API for PHP?

My projet is based on PHP.
Is there and SDK for it?
Are there exaples using PHP?

Hi @shlomo1. Pinecone doesn’t offer an official PHP SDK, but it looks like there are some community-supported SDKs out there. Pinecone hasn’t tested any of them, but at first glance, this one looks more up-to-date than some others: GitHub - probots-io/pinecone-php: A beautiful, extendable PHP Package to communicate with your pinecone.io indices, collections and vectors..

Interested to hear if that works for you. If not, it’s always an option to use the API directly with curl, for example:

<?php

$curl = curl_init();

curl_setopt_array($curl, [
  CURLOPT_URL => "https://api.pinecone.io/indexes",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\n  \"deletion_protection\": \"enabled\",\n  \"dimension\": 1536,\n  \"metric\": \"cosine\",\n  \"name\": \"movie-recommendations\",\n  \"spec\": {\n    \"serverless\": {\n      \"cloud\": \"gcp\",\n      \"region\": \"us-east1\"\n    }\n  }\n}",
  CURLOPT_HTTPHEADER => [
    "Api-Key: <api-key>",
    "Content-Type: application/json"
  ],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Best,
Jesse

Thanks for these suggestions. The “probots-io/pinecone-php” has partially worked for me. I’m using CakePHP and can successfully create, update, and delete the desired vectors in my Pinecone indexes. However, the query() method ( GitHub - probots-io/pinecone-php: A beautiful, extendable PHP Package to communicate with your pinecone.io indices, collections and vectors.) isn’t giving me any data back. Any idea why? I’m on the verge of using your cURL suggestion for that instead, but I’m afraid it will end up the same.

Hi @Nick.V.

That’s odd. I wonder: Did you upsert your vectors into a specific namespace in your index? If so, you need to target that namespace in your query. Otherwise, the query targets the default namespace.

Jesse

No I have been using the default namespace the whole time. It might even be giving me the data but I don’t know how to read it. For example, the attached image shows what I get if I use “die(dd(<results_go_here>))” immediately after using the “query()” method. I’ve scoured through those results, but no luck.

I’m not experience with PHP, so I’m not sure what’s happening. But for what it’s worth, I installed the PHP SDK and created a script that checks the stats on an index, queries the index by id, and then fetches a few records by ID, and it’s working fine for me. Here’s the code, in case it’s helpful:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

require __DIR__ . '/vendor/autoload.php';

use \Probots\Pinecone\Client as Pinecone;

$apiKey = 'API_KEY';

// Initialize Pinecone
$pinecone = new Pinecone($apiKey);

// Target the index
$pinecone->setIndexHost('INDEX_HOST');

// Get index stats
$response = $pinecone->data()->vectors()->stats();

if($response->successful()) {
    echo "Vector Statistics:\n";
    var_dump($response->json());
} else {
    echo "Error occurred:\n";
    var_dump($response->body());
    var_dump($response->status());
}

$response = $pinecone->data()->vectors()->query(
    id: "kirengeshoma-palmata",
    topK: 1,
    includeValues: false,
    includeMetadata: true,
);

if($response->successful()) {
    echo "Query results:\n";
    var_dump($response->json());
} else {
    echo "Error occurred:\n";
    var_dump($response->body());
    var_dump($response->status());
}

$response = $pinecone->data()->vectors()->fetch([
    'kirengeshoma-palmata', 'monarda-bee-balm'
], 'flowers');

if($response->successful()) {
    echo "Fetch results:\n";
    var_dump($response->json());
} else {
    echo "Error occurred:\n";
    var_dump($response->body());
    var_dump($response->status());
}

Hi @jesse just wondering; will there be any official php sdk by pinecone in the foreseeable future?

Hi @rodney - product manager for the SDKs and API here. While a PHP SDK is not currently on our roadmap, there are a few options.

The first option is to leverage the community built SDK that Jesse linked above. The second option is to leverage our OpenAPI specs, which are newly public in this repository.

Thanks!

Good news! I finally figured out how to make cURL work for me, in part due to your help. I still need to do some more testing, but it appears that this finally gives me the desired data back.

1 Like