I have the below code to create invoice for order in magento 2.0
<?php
$adminUrl='http://xxxxx/index.php/rest/V1/integration/admin/token';
$ch = curl_init();
$data = array("username" => "xxxxx", "password" => "xxxxx");
$data_string = json_encode($data);
$ch = curl_init($adminUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$token = curl_exec($ch);
$token= json_decode($token);
$headers = array("Authorization: Bearer $token");
$requestUrl1='http://xxxxx/index.php/rest/V1/invoices';
$ch1 = curl_init();
$ch1 = curl_init($requestUrl1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result1 = curl_exec($ch1);
$result1= json_decode($result1);
print_r($result1);
?>
It was giving me
{"message":"%fieldName is a required field.","parameters":{"fieldName":"entity"}}
try to pass Invoice data like this
$setHaders = array('Content-Type:application/json','Authorization:Bearer '.$adminToken);
$productData='{"entity":{"orderId":6,"items":[{"orderItemId":9,"qty":1},{"orderItemId":10,"qty":1}],"comments":[],"extensionAttributes":{}}}';
$requestURL= "{magento-url}/rest//V1/invoices";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $requestURL);
curl_setopt($ch,CURLOPT_POSTFIELDS, $productData);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $setHaders);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
Related
The below code works with API URL: https://api2.example.com/service/vps/list
but the API provider has changed, they use 2 URLs at the same time, for example: https://api2.example.com/service/vps/list and https://api2.example.com/service/dedicated/list
how to get vps and dedicated information from the above two links?
this is my code working with https://api2.example.com/service/vps/list only:
if (!defined("WHMCS"))
die("This file cannot be accessed directly");
use Illuminate\Database\Capsule\Manager as Capsule;
class FKL
{
public $apikey = '';
public $apiurl = 'https://api2.example.com/';
public function __construct($apikey = '')
{
$this->apikey = $apikey;
}
public function getList()
{
$sendparams = [];
$sendparams['APIKey'] = $this->apikey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->apiurl . "service/vps/list");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($sendparams, JSON_PRETTY_PRINT));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json')
);
$result = curl_exec($ch);
curl_close($ch);
if ($result) {
$list = json_decode($result, true);
return $list['data'];
}
}
public function getOSList()
{
$sendparams = [];
$sendparams['APIKey'] = $this->apikey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->apiurl . "service/vps/os");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($sendparams, JSON_PRETTY_PRINT));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json')
);
$result = curl_exec($ch);
curl_close($ch);
if ($result) {
$list = json_decode($result, true);
return $list['data'];
}
}
......
Thank you!
I have added code:
curl_setopt($ch, CURLOPT_URL, $this->apiurl . "service/dedicated/list");
bellow:
curl_setopt($ch, CURLOPT_URL, $this->apiurl . "service/vps/list");
and now are working!
This works from the command line how do I convert this to PHP I have tried anything but nothing seems to work.
curl -H "Authorization: Bearer APIKEY" https://www.gocanvas.com/apiv2/forms.xml -F 'username=XXXXXXX#XXXXXXXXXX.com'
Here is a function I have tried.
function getgocan(){
$url = 'https://www.gocanvas.com/apiv2/forms.xml?username=XXXXX#XXXXXXXXXX.com';
$ch = curl_init();
$jsonData = array(
'text' => ''
);
$jsonDataEncoded = json_encode($jsonData, true);
$header = array();
$header[] = "APIKEY";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
}
-F sends form fields in the POST data, not URL query parameters. So you need to put username=XXX into CURLOPT_POSTFIELDS, and it shouldn't be JSON.
You can give an associative array to CURLOPT_POSTFIELDS, and it will encode it automatically.
function getgocan(){
$url = 'https://www.gocanvas.com/apiv2/forms.xml';
$ch = curl_init();
$params = array(
'username' => 'username=XXXXX#XXXXXXXXXX.com'
);
$header = array(
'Authorization: Bearer APIKEY'
);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
curl_close($ch);
var_dump($$result);
}
I'm trying to develop a PHP client to interact with HBase REST API. But I can't find the way to create scanner using JSON request. All examples I could find are using XML format. Just wonder if it's possible to send using JSON format.
The code below returned error message:
"HTTP/1.1 500 Can not deserialize instance of java.lang.String out of START_ARRAY token"
<?php
$headers = array(
'Accept: application/json',
'Content-Type: application/json',
);
if (!file_exists(__DIR__ . '/tmp')) {
mkdir(__DIR__ . '/tmp', 0777);
}
$scannerFile = __DIR__ . '/tmp/scanner';
if (!file_exists($scannerFile)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://hbase_uri:20550/news/scanner");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_GSSNEGOTIATE);
curl_setopt($ch, CURLOPT_USERPWD, ":");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array(
'batch' => 10,
'filter' => array(
array(
'type' => 'PrefixFilter',
'value' => 'u123',
),
),
)));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($ch);
file_put_contents($scannerFile, $page);
curl_close($ch);
} else {
$s = file_get_contents($scannerFile);
$parts = explode('Location: http://hbase_uri:20550/news/scanner/', $s);
$parts = explode("\n", $parts[1]);
$parts[0] = trim($parts[0]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://hbase_uri:20550/news/scanner/" . $parts[0]);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_GSSNEGOTIATE);
curl_setopt($ch, CURLOPT_USERPWD, ":");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($ch);
file_put_contents(__DIR__ . '/tmp/result', $page);
curl_close($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://hbase_uri:20550/news/scanner/" . $parts[0]);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_GSSNEGOTIATE);
curl_setopt($ch, CURLOPT_USERPWD, ":");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($ch);
file_put_contents(__DIR__ . '/tmp/result_delete', $page);
curl_close($ch);
}
I got the answer by other's hint:
<?php
$headers = array(
'Accept: application/json',
'Content-Type: application/json',
);
if (!file_exists(__DIR__ . '/tmp')) {
mkdir(__DIR__ . '/tmp', 0777);
}
$scannerFile = __DIR__ . '/tmp/scanner';
if (!file_exists($scannerFile)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://hbase_uri:20550/news/scanner");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_GSSNEGOTIATE);
curl_setopt($ch, CURLOPT_USERPWD, ":");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array(
'batch' => 10,
'filter' => json_encode(array(
'type' => 'PrefixFilter',
'value' => 'u123',
)),
)));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($ch);
file_put_contents($scannerFile, $page);
curl_close($ch);
} else {
$s = file_get_contents($scannerFile);
$parts = explode('Location: http://hbase_uri:20550/news/scanner/', $s);
$parts = explode("\n", $parts[1]);
$parts[0] = trim($parts[0]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://hbase_uri:20550/news/scanner/" . $parts[0]);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_GSSNEGOTIATE);
curl_setopt($ch, CURLOPT_USERPWD, ":");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($ch);
file_put_contents(__DIR__ . '/tmp/result', $page);
curl_close($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://hbase_uri:20550/news/scanner/" . $parts[0]);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_GSSNEGOTIATE);
curl_setopt($ch, CURLOPT_USERPWD, ":");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($ch);
file_put_contents(__DIR__ . '/tmp/result_delete', $page);
curl_close($ch);
}
For the future visitors.
Using #kiang answer I have successfully made a CURL POST creating an scanner:
curl \
--verbose \
--request POST \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data '{"batch":10,"filter":"{\"type\":\"PrefixFilter\",\"value\":\"u123\"}"}' \
'https://hbase:20550/namespace:table/scanner/'
This returned
Location -> Buffer(https://hbase:20550/namespace:table/scanner/151302168977413777789)
...
AhcWSResponse(StandaloneAhcWSResponse(201, Created))
What might be useful:
Using those examples (you need to escape " signs)
Finding the docs of ScannerModel.
Profile Sharing Server-Side Integration (PayPal-iOS-SDK)
Retrieve Customer Information Using a Valid Access Token. but it is not working (customer Information not fetch what is issue in my code. if any idea and code please help me )
$access_token = "xxxx";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/identity/openidconnect/userinfo/?schema=openid");
curl_setopt($ch, CURLOPT_HEADER, "Content-Type:application/json");
curl_setopt($ch, CURLOPT_HEADER, "Authorization: Bearer ".$access_token);
$result1 = curl_exec($ch);
curl_close($ch);
print_r($result1);die;
$access_token = "xxxxx";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/identity/openidconnect/userinfo?schema=openid&access_token=xxxx");
$headers = array("Accept: application/json", "Accept-Language: en_US");
curl_setopt($ch, CURLOPT_HEADER, $headers);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HEADER, "Authorization: Bearer ".str_replace(" ", "", $access_token));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result1 = curl_exec($ch);
$information = curl_getinfo($ch);
curl_close($ch);
print_r($information);die;
https://gist.github.com/xcommerce-gists/4007896#file-getuserprofile-request-curl
We are trying to convert use the following curl cmdline which is working fine
curl --request POST --data-binary #"/home/project/enrol/my.wav" --header "Content-Type:audio/wav" --header "VsitEmail: 200037test2#test.com" --header "VsitPassword: 8270f2824111e04d9278c01a92b388147d9d02e0b50d946d25d00db375ff1282" --header "VsitDeveloperId: 200037" https://siv.voiceprintportal.com/sivservice/api/enrollments
to PHP libcurl which is constantly giving internal error.
Please suggest where i am wrong .
Code
$ch = curl_init();
$path="/home/project/enrol/my.wav";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: audio/wav","VsitEmail: 200037test2#test.com","VsitPassword: 8270f2824111e04d9278c01a92b388147d9d02e0b50d946d25d00db375ff1282","VsitDeveloperId: 200037"));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt( $ch , CURLOPT_VERBOSE , 0 );
curl_setopt($ch, CURLOPT_POSTFIELDS,array('file' => '#'.$path));
$result = curl_exec($ch);
//close connection
curl_close($ch);
print_r($result);
Update 1
$localfile='/home/project/enrol/my.wav';
$url = 'https://siv.voiceprintportal.com/sivservice/api/enrollments';
$ch = curl_init();
$fields = array('file' => '#' .$localfile);
$resource = curl_init();
curl_setopt($resource, CURLOPT_URL, $url);
//curl_setopt($resource, CURLOPT_HTTPHEADER, $header);
curl_setopt($resource, CURLOPT_HTTPHEADER, array("Content-Type:
audio/wav","VsitEmail: 200037test2#test.com","VsitPassword: 8270f2824111e04d9278c01a92b388147d9d02e0b50d946d25d00db375ff1282","VsitDeveloperId: 200037"));
curl_setopt($resource, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($resource, CURLOPT_POST, 1);
curl_setopt($resource, CURLOPT_BINARYTRANSFER, true);
curl_setopt($resource, CURLOPT_POSTFIELDS, $fields);
//$result = json_decode(curl_exec($resource));
$result=curl_exec($resource);
curl_close($resource);
echo "The result is";
print_r($result);
Check the following code
$url = 'http://google.com';
$header = array('Content-Type: multipart/form-data');
$fields = array('file' => '#' . $_FILES['file']['tmp_name'][0]);
$resource = curl_init();
curl_setopt($resource, CURLOPT_URL, $url);
curl_setopt($resource, CURLOPT_HTTPHEADER, $header);
curl_setopt($resource, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($resource, CURLOPT_POST, 1);
curl_setopt($resource, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($resource);
curl_close($resource);
It got resolved by passing the raw data file instead of an array. Correct code is
$localfile='#/home/project/enrol/my.wav';
$url = 'https://siv.voiceprintportal.com/sivservice/api/enrollments';
$data = file_get_contents('/home/project/enrol/my.wav');
$headers = array();
$headers[] = 'X-Requested-With: JSONHttpRequest';
$headers[] = 'Content-Type: audio/wav';
$headers[] = 'VsitEmail: 200037test2#test.com';
$headers[] = 'VsitPassword: 8270f2824111e04d9278c01a92b388147d9d02e0b50d946d25d00db375ff1282';
$headers[] = 'VsitDeveloperId: 200037';
$resource = curl_init();
curl_setopt($resource, CURLOPT_URL, $url);
curl_setopt($resource, CURLOPT_HTTPHEADER,$headers);
curl_setopt($resource, CURLOPT_POST,1);
curl_setopt($resource, CURLOPT_BINARYTRANSFER, true);
curl_setopt($resource, CURLOPT_POSTFIELDS,$data);
$result = json_decode(curl_exec($resource));
curl_close($resource);
echo "The result is";
print_r($result);