1

我正在尝试使用 VEND API,但我不知道如何传递参数来做各种事情。我在 API 文档中找到了一组代码,但无法通过它。以下是我一直在做的事情。

<?php
/**
*
* A class made for utility methods around Vend's API
*
*
*/
class VendApiUtils {

/**
* A simple static method to call Vend API urls
*
* @param string $url
* @param string $username
* @param string $password
* @param array $params <OPTIONAL>
*/
public static function requestVendApiUrl($url, $username, $password, $params = array())
{
// is cURL installed?
if (!function_exists('curl_init')) {

throw new Exception('CURL is not installed!');
}

// Create a new cURL instance
$ch = curl_init();

$opts = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array('data' => json_encode($params)),
CURLOPT_TIMEOUT => 120,
CURLOPT_FAILONERROR => 1,
CURLOPT_HTTPAUTH => CURLAUTH_ANY, 
CURLOPT_USERPWD => "$username:$password" 
);

// Assign the cURL options
curl_setopt_array($ch, $opts);

// Get the response
$response = curl_exec($ch);
print_r($response);

// Close cURL
curl_close($ch);

// Execute the request & get the response
echo $response;
}
}


$your_domain='https://mydomain.vendhq.com';
$your_username='myuser'; 
$your_password='mypass';
$params = array('GET'=>'/api/stock_movements');
$response =  VendApiUtils::requestVendApiUrl($your_domain, $your_username,$your_password, $params);

//echo $response;
?>

任何帮助都会受到高度赞赏。

问候, 阿纳布

4

2 回答 2

0

我不确定您为什么认为 URL 需要进入$params数组;在我看来,这就像是 params 的地方。我认为该功能旨在更像这样使用:

// Fixed parts, same for any request
$domain='https://mydomain.vendhq.com';
$username='myuser'; 
$password='mypass';

// Variable parts, depending on what you want to do
$url = '/api/stock_movements';
$params = array('some_key' => 'some_value');

$response = VendApiUtils::requestVendApiUrl($domain . $url, $username, $password, $params);
于 2013-05-05T23:26:33.457 回答
0

请检查pyvend客户端库。

于 2014-11-20T10:51:01.833 回答