Is it possible to process woocommrce api using curl?
I am trying to do it but no success. This api works in insomnia or postman
To Process
curl https://example.com/wp-json/wc/v3/products -u consumer_key:consumer_secret
Following is what I am doing
https://www.example.com/wp-json/wc/v3/products
$Consumer_Key="ck_111111";
$Consumer_Secret= "cs_222222";
$options = array(
CURLOPT_URL => $URL,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_USERPWD => $Consumer_Key.":".$Consumer_Secret
);
$ch=curl_init();
curl_setopt_array($ch, $options);
// Execute request, store response and HTTP response code
$response=curl_exec($ch);
curl_close($ch);
print_r($response);
And The error I am getting is
Forbidden
You don't have permission to access this resource.
Additionally, a 403 Forbidden
error was encountered while trying to use an ErrorDocument to handle the request.
I don't understand a lot of PHP but I have the same code working well on Angular with the cURL:
getCategories() {
this.http.get(this.cUrl + "/wp-json/wc/v3/products/categories?per_page=100&consumer_key=" + this.wooApiClie + "&consumer_secret=" + this.wooApiSec).subscribe(res => {
this.categories = res;
console.log(this.categories);
})
This one is for the categories but it's the same for the products. Maybe it's because your variables $Consumer_key and $Consumer_secret are in caps? I repeat, I don't understand too much of PHP.
Related
I'm trying to use the discogs php api in a PHP script to get info on a release using the release ID. For example, when I make a request to:
http://mywebsite.com/test.php?id=1017868
I want to call the discogs API to get info on the release with id = 1017868. I can see the info I want by manually going to:
https://api.discogs.com/releases/1017868
So I have my $consumerKey =and $consumerSecret and I'm following the DISCOGS AUTH FLOW instructions, which says I can send my keys in a get request like so:
curl "https://api.discogs.com/database/search?q=Nirvana" -H "Authorization: Discogs key=foo123, secret=bar456"
I'm trying to make a get request for my target id in my php script like so:
<?php
echo "hello world <br>";
//discogs simple auth flow, http requests
$remote_url = 'https://api.discogs.com/releases/1017868';
// Create a stream
$opts = array(
'http'=>array(
'method'=>"GET",
'header' => "Authorization: Discogs key=mykeyasdlkhaskld, secret=mysecretkeykjnasdkjnsadkj"
)
);
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
$file = file_get_contents($remote_url, false, $context);
print($file);
echo "end of program";
?>
But I keep getting the error:
"failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden".
Is there something I'm forgetting for making my request to the server? Thanks.
Everything I look at online is showing how to use OAuth & Curl to make a POST request, but I want to make a get request to the Mailchimp API and I'm not getting any response it seems. I've already managed to go through the authentication and get the user's token & api URL. Now I'm just trying to pull in their lists. Here's the CURL code I've got currently:
$headers = array(
"Content-type: application/json",
"Authorization: OAuth ".$user['mct']
);
$curl = curl_init();
curl_setopt_array($curl,array(
CURLOPT_URL => "https://".$user['dc'].".api.mailchimp.com/3.0/lists",
CURLOPT_USERAGENT => "oauth2-draft-v10",
CURLOPT_HTTPHEADER => $headers,
CURLOPT_ENCODING => ''
));
$tresp = curl_exec($curl);
$lists = json_decode($tresp,true);
curl_close($curl);
Assuming $user['mct'] and $user['dc'] contain the proper values, any idea what I'm doing wrong here?
In case anyone ends up googling and finding this, my problem was that the user information I was getting from wordpress' get_results() function was an object and not an array. Took me forever to realize because for some reason this part of my plugin is preventing me from using print_r().
Now that it's actually going to the Mailchimp API I'm able to get and debug whatever error they're sending back.
We are having a RESTful API and RESTful clients, both in PHP. Client connecting to server via cURL http requests.
$handler = curl_init (self::API_ENDPOINT_URI . $resource);
$options =[
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_TIMEOUT => 6000,
];
curl_setopt_array ($handler, $options);
$result = curl_exec ($handler);
curl_close ($handler);
Then in model somewhere we call it:
$request = json_decode($this->_doRequest('/client/some_id'));
There is a JSON response and we parse it. Everything is ok till... Till some users start creating multiple requests and PHP hangs. For example we have a client page which is making ~5 requests to API server. When user opens a 10 tabs in browser with 10 different clients it's ~50 requests which are going one by one. That means that before first tab won't finish his work other tabs won't start their work.
Is it any way to fix this issue in simple way?
We would like to use cURL multi handler for this but not sure how to get responses immediately.
Thanks.
I have to do a DELETE request, with parameters, in the CodeIgnitor platform. First, I tried using cURL, but I switched to Guzzle.
An example of the request in the console is:
curl -X DELETE -d '{"username":"test"}' http://example.net/resource/id
But in the documentation of Guzzle they use parameters just like GET, like DELETE http://example.net/resource/id?username=test, and I don't want to do that.
I tried with:
$client = new GuzzleHttp\Client();
$client->request('DELETE', $url, $data);
but the request just calls DELETE http://example.com/resource/id without any parameters.
If I interpret your curl request properly, you are attempting to send json data as the body of your delete request.
// turn on debugging mode. This will force guzzle to dump the request and response.
$client = new GuzzleHttp\Client(['debug' => true,]);
// this option will also set the 'Content-Type' header.
$response = $client->delete($uri, [
'json' => $data,
]);
coming late on this question after having same.
Prefered solution, avoiding debug mode is to pass params in 'query' as :
$response = $client->request('DELETE', $uri, ['query' => $datas]);
$datas is an array
Guzzle V6
$response = json_decode($this->client->delete($uri,$params)->getStatusCode());
echo $response;
This will also give the status of the response as 204 or 404
I noticed that Laravel has a neat method Request::wantsJson - I assume when I make the request I can pass information to request a JSON response, but how do I do this, and what criteria does Laravel use to detect whether a request asks for JSON ?
It uses the Accept header sent by the client to determine if it wants a JSON response.
Let's look at the code :
public function wantsJson() {
$acceptable = $this->getAcceptableContentTypes();
return isset($acceptable[0]) && $acceptable[0] == 'application/json';
}
So if the client sends a request with the first acceptable content type to application/json then the method will return true.
As for how to request JSON, you should set the Accept header accordingly, it depends on what library you use to query your route, here are some examples with libraries I know :
Guzzle (PHP):
GuzzleHttp\get("http://laravel/route", ["headers" => ["Accept" => "application/json"]]);
cURL (PHP) :
$curl = curl_init();
curl_setopt_array($curl, [CURLOPT_URL => "http://laravel/route", CURLOPT_HTTPHEADER => ["Accept" => "application/json"], CURLOPT_RETURNTRANSFER => true]);
curl_exec($curl);
Requests (Python) :
requests.get("http://laravel/route", headers={"Accept":"application/json"})