Curl PHP API Post - php

i am wanting to post to an API in woocommerce php file i used code below.
$ch = curl_init("https://cors-anywhere.herokuapp.com/https://api.feverfinance.co.za/FTIntegration.svc/BalanceLookup");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Basic '. base64_encode( $username .":" .$password ) ,
'Accept: application/json, text/plain, */*' ,
'Content-Length: ' . strlen($data_string))
);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT , 0);
$result = curl_exec($ch);
// Check if any error occurred
if(curl_errno($ch))
{
print_r($ch);
print_r($result);
}
$obj = json_decode($result,true);
//Mage::log($result,true);
$tranactionSuccess = $obj['Success'];
$transactionMessage = $obj['Message'];
curl_close($ch);
when i print to console i get ErrorResource id #6.
Please can anyone advise me how to post to API in woocommerce. with the above information that is needed to post to the API
Regards

First you need to activate woocommerce APIs from wordpress backend and generate API keys
Check https://docs.woocommerce.com/document/woocommerce-rest-api/
Then use the following code based on your needs
Example: You need to retrieve product details
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://example.com/wp-json/wc/v3/products/794");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_USERPWD, "consumer_key" . ":" . "consumer_secret");
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
Note: consumer_key and consumer_secret was generated by API from backend
and 794 mean product ID
API docs for woocommerce http://woocommerce.github.io/woocommerce-rest-api-docs/

Related

Google Drive trash Clear Using Php not works

I am trying to clear Trash using php and drive api v3. Code excutes with no error but trash in drive is not deleted. Here is my code please help me to fix this. Thanks For your time.
if($_POST['trash']) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.googleapis.com/drive/v3/files/trash?key='.$token.'");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/json',
'Authorization: Bearer ' . $token
));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
};
I believe your goal and current situation as follows.
You want to use "Files: emptyTrash" in Drive API v3 using curl of php.
Your access token of $token can be used for using this API.
In order to achieve your goal, how about the following modification?
From:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.googleapis.com/drive/v3/files/trash?key='.$token.'");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/json',
'Authorization: Bearer ' . $token
));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
To:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.googleapis.com/drive/v3/files/trash");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $token));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
Note:
At "Files: emptyTrash", no values are returned.
If $token in your script is the API key which is not the access token, please retrieve the access token and use it. Please be careful this.
Reference:
Files: emptyTrash

getting json response from protected server

How can i connect to this url
curl 'https://feeds.listhub.com/pickup-v2/<your account id>/' -H 'Content-Type: Application/json' --user <your_account_id>
I replaced and in both replaces.
Here is the documentation that they have sent me
https://developer.listhub.com/faq/#how-often-should-we-update-the-feed
when i try this from php curl i get a default get of containing zip files instead of a json response
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_POSTREDIR, 3);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow http 3xx redirects
$response = curl_exec($ch); // execute
// echo '<h2>API Response</h2>';
// print_r($resp_orders);
$resultStatus = curl_getinfo($ch);
if($resultStatus['http_code'] == 200) {
echo json_decode($response);
} else {
echo 'Call Failed '.print_r($resultStatus);
}
if i directly echo $response i get default webpage but i echo json_decode i get nothing..
There is no password.
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
should just be
curl_setopt($ch, CURLOPT_USERPWD, $username);
Where $username is equal to your_account_id
And you also need to set the header
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: Application/json',
));
Hope that helps.

Ebay rest api not working for my sandbox account

I'm using sandbox and i have generated
Key and token
I have my seller sandbox account .
I'm trying to use sell api (inventory and fullfillment ),but its giving as "A resource (URI) associated with the request could not be resolved" with an error code 3003.
This is the git hub package i'm using in my code .
<?php
// Your token
$authToken = 'AgAAAA**AQAAAA**aAAAAA**MJNfXQ**nY+sHZ2PrBmdj6wVnY+sEZbIEFTWmW7eYbvzFw0uXajYBe6j5ddEO3crLb0oCIHHhc3UFOFYX4+mzwE/6HboGAezWiTXbfJvi034ScYcE0MJ08YHjIt3ciNyu/7OHUnbedLVhNqYjGt/oXL/9bvQPHFcGH/+XS1KfNV+XcdZV/2HJ8n+QNhYawC3lIKeKm6rsH1HH3x9ZECNYvGzPTjL23uaTH53UoshKYPFTi/uq7UO4ZNccE/NdreamfAzGiBTAz6pqL+SnFLrOVn9llPNlMGLO/yVEkAFIdwDF2KduXPVV7ZFTVX9ncfdX04r80Jx4LiMIEnhq5Ftjwq48q+arkDy8Y4T+bf6YPE5UICJnXyGAkJJhSZa5hliV+TRgSFRQkBhCVJf+ZAvVO4CA/TRZxtH77YTSj/Z9HGSzDpavFfh+7IM2lmQxCoHmhHI7e8pta6QBMvKOuq65SH17IW0bYog76dFsDa5OjnjFmFMU4u4dzF9lTFm8lw0TTqjnGja8mh5CZA4OBSsVgkqq4diWacZXKQw5p1tofG4aYbQJ8XNnYBPOAyH2fuSlWmY4Lj+RpBbVmhtj5RA/h/MKUmgN+USGdT0DeKWQMCTVmNaQFJbSEQEzdSRBBQhvqubhnqPG4/95gwanPOxasEbQxMUpnWnF4oOE+eS432P9ILTSOnEZ7Qs70R/GcilczhaPUDJUH9+dFp4DMNVT2P1tiflmBAt';
$header = array('Accept: application/json',
'Authorization: Bearer '.$authToken,
'Content-Type: application/json'
);
$placeOrderUrl =
"https://api.sandbox.ebay.com/sell/fulfillment/v2/order/110181400870-
27973775001";
$body = '{}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, trim($placeOrderUrl));
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_POSTFIELDS,$body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$response = curl_exec($ch);
$information = curl_getinfo($ch);
print_r($response);
?>
I needs to pull the orders as in the doc for the rest api .
I don't know where i'm stuck . Thanks in advance if any one can help me out .

php cuRL response "unable to transcode data stream audio/flac -> audio/x-float-array" - IBM Watson Speech to text API

I don't know much about how to use cURL.I am trying to convert Speech to Text using IBM Watson API. When I try to convert it without using parameters(Translate English
Audio File), I get a response without any error.
But when I add
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'model'=>'ja-JP_NarrowbandModel'
))
It returns
{ "code_description": "Bad Request", "code": 400, "error": "unable to
transcode data stream audio/flac -> audio/x-float-array " }
I am not sure if there is an issue in my Syntax or something else is going wrong there.
I read docs from : https://console.bluemix.net/docs/services/speech-to-text/http.html#http
<?php
$ch = curl_init();
$file = file_get_contents('audio-file.flac');
curl_setopt($ch, CURLOPT_URL, 'https://stream.watsonplatform.net/speech-to-text/api/v1/recognize');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $file);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'apikey' . ':' . 'MY_API_HERE');
$headers = array();
$headers[] = 'Content-Type: audio/flac';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'model'=>'ja-JP_NarrowbandModel'
));
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
print_r($result);
You are setting CURLOPT_POSTFIELDS twice, once with the content of your file and a second time with an array containing 'model'=>'ja-JP_NarrowbandModel'.
According to the documentation, you can pass the model as a query parameter.
Try something like this (not tested):
<?php
$file = file_get_contents('audio-file.flac');
$url = 'https://stream.watsonplatform.net/speech-to-text/api/v1/recognize';
$model = 'ja-JP_NarrowbandModel';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . '?model=' . $model);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $file);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'apikey' . ':' . 'MY_API_HERE');
$headers = array();
$headers[] = 'Content-Type: audio/flac';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
print_r($result);

Curl not working in wp

I have this request in wordpress,
$url = $this->apiUrl . '/' . $url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json',
'X-Client: PHP ' . phpversion()
));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERPWD, ":" . $this->apiKey);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
switch ($httpVerb) {
case 'post':
curl_setopt($ch, CURLOPT_POST, true);
if ($data) {
$encoded = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded);
}
break;
}
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo json_encode(curl_getinfo($ch));
curl_close($ch);
And this is the response
{"url":"https:\/\/api.paylike.io\/cards\/5a05ba1d752c916c2607c5fb","content_type":null,"http_code":400,"header_size":435,"request_size":242,"filetime":-1,"ssl_verify_result":0,"redirect_count":0,"total_time":0.395293,"namelookup_time":0.01238,"connect_time":0.01387,"pretransfer_time":0.031095,"size_upload":0,"size_download":0,"speed_download":0,"speed_upload":0,"download_content_length":-1,"upload_content_length":-1,"starttransfer_time":0.395242,"redirect_time":0,"redirect_url":"","primary_ip":"104.20.8.147","certinfo":[],"primary_port":443,"local_ip":"138.68.167.102","local_port":45414}
The link is ok, but I don't know why do I get an 400 error, try accessing the page and see that is working :
https://api.paylike.io/cards/5a05ba1d752c916c2607c5fb
So where is the error, does wordpress have some limitation ? I tried this code outside of wordpress, and it works. Also I tried spootify my curl, but no results ...
Firstly check if the curl is enabled in your system or server. You can check it by using a simple page with this code.
phpinfo();
If curl is enabled it will show there. If its not enabled enable it.
Next check for the curl errors and clear it. Just add this code after the curl_exec($ch);
'Error Details: ' . curl_error($ch);
It will show you the error.

Categories