WooCommerce rest api not getting curl response - php

When I try to run WooCommerce API in Postman it shows me json response, but when i try to run it will CURL, it doesn't giving me any response and loading, can anyone please help me what is issue of it ?
$header = array(
'Authorization: Basic Y2tfZmE1NWQ1N2RjNmY1xxxxxx'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://xxxxx.com//wp-json/wc/v3/products/attributes');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
//curl_setopt($ch, CURLOPT_POST, 0);
//curl_setopt($ch, CURLOPT_POSTFIELDS, "postvar1=value1&postvar2=value2&postvar3=value3");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
if (curl_error($ch)) {
$error_msg = curl_error($ch);
echo $error_msg; die;
}
echo "<pre>";
print_r($server_output);
die;
curl_close($ch);

Related

Getting JSON data from API with PHP and curl

There is an API page and I want to get the whole API data with the help of PHP+curl
For example the url is https://example.com/api which contains the following:
{"data": [{"information":{"username": "john","id": 15}]}
I want to have on my php result page: {"information":{"username": "john","id": 15}
using GET method.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://example.com/api');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
print_r($result);
curl_close($ch);
?>
and I get white page.Where is the error?

Connecting to VCenter with PHP using REST API authentication error

I followed the instructions in the official vsphere site to get info from the server and from the answer of another user here .
From what I have understood, firstly I have to get the session id (cis id), but I got "null" as a result.
I tried multiple codes but the result is the same:
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$headers = array(
'Content-Type: application/json',
'Accept: application/json',
'vmware-use-header-authn: test'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, "https://vcsa/rest/com/vmware/cis/session");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'administrator#vs.dom:userpassword');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
$out = json_decode(curl_exec($ch));
// var_dump($out);
if ($out === false) {
echo 'Curl Error: ' . curl_error($ch);
exit;
}
$sid = $out;
dd($out);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("vmware-api-session-id:$sid"));
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, "https://vcsa/rest/vcenter/vm");
$output = curl_exec($ch);
$vms = json_decode($output);
var_dump($vms);
curl_close($ch);
The result is null.
this is a script word 100% using PHP
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, "https://{ip}/rest/com/vmware/cis/session");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'username' . ":" . 'password');
$out = json_decode(curl_exec($ch));
// var_dump($out);
if ($out === false) {
echo 'Curl Error: ' . curl_error($ch);
exit;
}
$sid = $out->value;
curl_setopt($ch, CURLOPT_HTTPHEADER, array("vmware-api-session-id:$sid"));
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, "https://{ip}/rest/vcenter/vm");
$output = curl_exec($ch);
$vms = json_decode($output);
var_dump($vms);
curl_close($ch);
?>
when you debug your code, the $out variable return null?
If yes, can you check if your curl returns 56 - OpenSSL SSL_read: Success error in $out = json_decode(curl_exec($ch));?
This error occoured with my code, and I see wich cURL in 7.67 version cause this trouble.
My solution was to downgrade cURL version to 7.65.
It works for me!
I had the same problem, and eventually understood that it is caused by this header:
curl_setopt($ch, CURLOPT_POST, true);
And replacing it with this one solves the issue:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');

PHP + Dropbox API V2 - show thumbnail

I'm trying to display five images from my Dropbox account through PHP.
Listing them works perfectly using https://api.dropboxapi.com/2/files/list_folder and curl.
The json output, I turn into a PHP array, which tells me what files I need.
Subsequently, I call the images using https://content.dropboxapi.com/2/files/get_thumbnail:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://content.dropboxapi.com/2/files/get_thumbnail");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CAINFO, "cacert.pem");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"path":"/images/1234.jpg","format":{".tag":"jpeg"},"size":{".tag":"w1024h768"}');
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Authorization: Bearer SECRET_CODE";
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
?>
According to documentation this returns some metadata and the main body is the image. Now, what I'm struggling with is how to display the image, as if you were directly accessing the image through the URL (so I can out it in an <img src="">).
I recon I need header('Content-Type: image/jpeg'); but I have no other clue on how to continue.
Any help would be greatly appreciated.
Best,
Knal
Get thumbnail request are via the header rather than a Post request. try
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://content.dropboxapi.com/2/files/get_thumbnail");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CAINFO, "cacert.pem");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$headers = array();
$headers[] = "Authorization: Bearer SECRET_CODE";
$headers[] = "Dropbox-API-Arg: {"path":"/images/1234.jpg","format":{".tag":"jpeg"},"size":{".tag":"w1024h768"}";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

Using curl in https

I tried accessing a website using curl and it just output Object moved to here. I used curl_error but doesn't show any errors.
$url = "https:somewebsite.com";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // Accepts all CAs
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
//curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
if(curl_error($ch))
{
echo 'error:' . curl_error($ch);
}
curl_close($ch);
echo $output;
?>

PHP - cURL. Error 501 Not Implemented

I getting error upon submitting a post request using cURL. So far i have this code:
$login_url = 'http://192.168.1.1/login';
$postArr = array(
'username'=>'username',
'password'=>'password',
'dst' => base_url().'login/home'
);
$this->curlPost($login_url, $postArr);
function curlPost($url, $data){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
echo 'using curl --- '.$url.'<br />';
var_dump($server_output);
}
Basically, this code should do POST username and password to specific url, but unfortunately it is returning "Error 501: Not Implemented" my server is wamp. Anyone have idea what is causing this issue? Thanks

Categories