php api connection code for bigcommerce not working - bigcommerce

I'm using Web Matrix on a Windows machine with composer, and also have uploaded the same code to a windows server.
i have written my code in PHP to connect to Bigcommerce API but it just won't connect!
<?php
echo "made it";
require 'vendor/autoload.php';
use Bigcommerce\Api\Client as Bigcommerce;
Bigcommerce::configure(array(
'store_url' => 'https://store-bwvr466.mybigcommerce.com',
'username' => 'demo',
'api_key' => 'df38dd10e9665a3cfa667817d78ec91ee9384bc3'
));
Bigcommerce_Api::setCipher('rsa_rc4_128_sha');
Bigcommerce_Api::verifyPeer(false);
$products = Bigcommerce_Api::getProducts();
foreach($products as $product) {
echo $product->name;
echo $product->price;
echo " at bottom";
?>
I've read the other posts, but nothing seems to help. The first echo statement runs, but nothing else, including the bottom echo.
Please don't tell me to use curl as it's murder to install in windows!
HELP!

You have to change Bigcommerce_Api to Bigcommerce
<?php
echo "start";
require "bigcommerce.php";
use Bigcommerce\Api\Client as Bigcommerce;
Bigcommerce::configure(array(
   'store_url' => 'https://store-bwvr466.mybigcommerce.com',
   'username' => 'demo',
   'api_key' => 'df38dd10e9665a3cfa667817d78ec91ee9384bc3'
   ));
Bigcommerce::setCipher('RC4-SHA');
Bigcommerce::verifyPeer(false);
$products = Bigcommerce::getProducts();
foreach($products as $product) {
   echo $product->name;
   echo $product->price;
}
echo "end";
?>

Related

How to search product with SKU in shopify using PHP cURL

I want to search product with SKU number using php cURL.
For ex: https://{secret kay}:{password}#{store:URL}/admin/api/{api-version}/products/search.json?query=sku:{sku}
The API you shared is correct, please use the code below to fetch the product based on SKU.
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://yourstore.myshopify.com/admin/api/2022-10/products/search.json?query=sku:ABC74357',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CUSTOMREQUEST => 'GET'
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
If you are using Linux, you must install the Apache server and PHP to run the above code, and for windows, you have to use the Xampp server.

API Question - How to create relationships and query with multiple GET calls using cURL

I was just handed a project and not sure how to handle it. I have limited API experience, but have been able to pull the data using the code below. In this example I want to display a cruise sailing. I have an API endpoint url of https://api.website.com/sailing/123456 (example). This gives me a ton of data, but not everything i need. For example, it gives me the "supplier_id" (this would be something like 22). I want the supplier_name, so I can display "Carnival Cruise" instead of "22". Supplier_name is in another endpoint url - https://api.website.com/suppliers. This is where I am stuck. My thought is to bring in multiple GETS and then query it down. For example, If I brought in both endpoint URLs in this example and relate the two using supplier_id (exists in both examples), then I could bring in my Supplier Name. Am I way off here? I'm used to working with a DB and queries, so maybe I'm on the wrong path. Please help if you can. Thanks!
<?php
/* Sample call to get cruise sailings with filter/fields constraints */
$curl = curl_init();
$api_endpoint = "https://api.website.com/sailing/123456";
$api_key = "a57fecd04e71402fba73cb2e63a262aa";
$dataArray = array(
"start_at" => 1,
"max_return" => 170
);
$request_url = $api_endpoint . "?" . http_build_query($dataArray);
curl_setopt_array(
$curl,
array(
CURLOPT_URL => $request_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"api_key: {$api_key}"
)
)
);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
/* SIMPLE DUMP OF DATA
echo $response;*/
}
$data = json_decode($response);
$total = $data->meta->total;
$startAt = $data->meta->start;
$maxReturn = $data->meta->count;
// Table view of data
echo '<table border=1><tr><th>Sailing ID</th><th>Title</th><th>Supplier ID</th><th>Supplier Name</th></tr>';
echo '<td>' .$item->sailing_id.'</td>';
echo '<td>' .$item->title.'</td>';
echo '<td>' .$item->supplier_id.'</td>';
echo '<td>' .$item->supplier_name.'</td></tr>';
echo '</table>';
?>

The result of the search query differs in the API and Shutterstock website

please help me.
The result of the search query differs in the API and Shutterstock website.
What am I doing wrong?
API returns only 4 results, but at shutterstock - thousands
My code:
$queryFields = [
"query" => "осень лес векторы"
];
$options = [
CURLOPT_URL => "https://api.shutterstock.com/v2/images/search?" . http_build_query($queryFields),
CURLOPT_USERAGENT => "php/curl",
CURLOPT_HTTPHEADER => [
"Authorization: Bearer xxxxx"
],
CURLOPT_RETURNTRANSFER => 1
];
$handle = curl_init();
curl_setopt_array($handle, $options);
$response = curl_exec($handle);
curl_close($handle);
$decodedResponse = json_decode($response);
echo "<pre>";
print_r( $decodedResponse );
echo "</pre>";
Thank you.
The default API access (Self Serve, or Free product) is limited to a subset of Shutterstock's library - only about 1 million images. That's why you only see 4 results for your search compared to the website. If you need access to more content, just reach out to the team - https://developers.shutterstock.com/contact-us

Setting up a Discord oauth2 login on my website (with PHP?)

So I'm having troubles learning how to set up a login through discord on my site. I've been browsing for literally hours and haven't been able to find anything I understand...
At the moment, I have created the discord application, giving me a client ID and client secret, as well as a link back to my localhost:
https://discordapp.com/api/oauth2/authorize?client_id=550631359337594881&redirect_uri=http%3A%2F%2Flocalhost&response_type=code&scope=identify
At the moment I have it set up to redirect from a button to that URL, which then sends me to discord to accept. It then returns me to http://localhost?code=CODE_HERE
However, I don't know what I am supposed to do with this code. I am trying to set it up so that it will show the person's username with hashtag thing, and their profile picture.
I am currently using HTML, CSS, JS, and PHP on the site, but I think I might need to use something else, but I don't know how to set that up, or what it is I need. I am running my local server with XAMPP. I'd prefer if it is just PHP, but I'm open to other options.
Does anyone know how I can convert the code to a username + image?
Thanks in advance!
Try this
Credits to: eslachance
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('max_execution_time', 300); //300 seconds = 5 minutes. In case if your CURL is slow and is loading too much (Can be IPv6 problem)
error_reporting(E_ALL);
define('OAUTH2_CLIENT_ID', '1234567890'); //Your client Id
define('OAUTH2_CLIENT_SECRET', 'verysecretclientcode'); //Your secret client code
$authorizeURL = 'https://discordapp.com/api/oauth2/authorize';
$tokenURL = 'https://discordapp.com/api/oauth2/token';
$apiURLBase = 'https://discordapp.com/api/users/#me';
session_start();
// Start the login process by sending the user to Discord's authorization page
if(get('action') == 'login') {
$params = array(
'client_id' => OAUTH2_CLIENT_ID,
'redirect_uri' => 'https://yoursite.location/ifyouneedit',
'response_type' => 'code',
'scope' => 'identify guilds'
);
// Redirect the user to Discord's authorization page
header('Location: https://discordapp.com/api/oauth2/authorize' . '?' . http_build_query($params));
die();
}
// When Discord redirects the user back here, there will be a "code" and "state" parameter in the query string
if(get('code')) {
// Exchange the auth code for a token
$token = apiRequest($tokenURL, array(
"grant_type" => "authorization_code",
'client_id' => OAUTH2_CLIENT_ID,
'client_secret' => OAUTH2_CLIENT_SECRET,
'redirect_uri' => 'https://yoursite.location/ifyouneedit',
'code' => get('code')
));
$logout_token = $token->access_token;
$_SESSION['access_token'] = $token->access_token;
header('Location: ' . $_SERVER['PHP_SELF']);
}
if(session('access_token')) {
$user = apiRequest($apiURLBase);
echo '<h3>Logged In</h3>';
echo '<h4>Welcome, ' . $user->username . '</h4>';
echo '<pre>';
print_r($user);
echo '</pre>';
} else {
echo '<h3>Not logged in</h3>';
echo '<p>Log In</p>';
}
if(get('action') == 'logout') {
// This must to logout you, but it didn't worked(
$params = array(
'access_token' => $logout_token
);
// Redirect the user to Discord's revoke page
header('Location: https://discordapp.com/api/oauth2/token/revoke' . '?' . http_build_query($params));
die();
}
function apiRequest($url, $post=FALSE, $headers=array()) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
if($post)
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$headers[] = 'Accept: application/json';
if(session('access_token'))
$headers[] = 'Authorization: Bearer ' . session('access_token');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
return json_decode($response);
}
function get($key, $default=NULL) {
return array_key_exists($key, $_GET) ? $_GET[$key] : $default;
}
function session($key, $default=NULL) {
return array_key_exists($key, $_SESSION) ? $_SESSION[$key] : $default;
}
?>
You have the code which is used to authenticate with many endpoints of the discord API. You need the http://discordapp.com/api/users/#me Endpoint. You authenticate with the Authorization Header. Take a look at the Developer Portal to find out more about your endpoint

bigcommerce api returning blank results

I have downloaded the bigcommerce API and created a test script using the get time sample.
I get a blank response & no error:
My test code is just:
<?php
require 'bigcommerce.php';
use Bigcommerce\Api\Client as Bigcommerce;
Bigcommerce::configure(array(
'store_url' => 'https://storeurl.mybigcommerce.com',
'username' => 'apiusername',
'api_key' => 'token'
));
Bigcommerce::setCipher();
Bigcommerce::verifyPeer(false);
$ping = Bigcommerce::getTime();
if ($ping) echo $ping->format('H:i:s');
?>
Can you assist as to why the result is blank ?
I think your cipher is incorrect.
Please look at the docs http://developer.bigcommerce.com/quickstarts/php
Bigcommerce::setCipher('RC4-SHA')