2013-05-05 50 views
0

我想使用VEND API,但我不知道如何傳遞參數做各種事情。我在API文檔中找到了一組代碼,但無法通過它。以下是我所做的。如何傳遞Vend 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; 
?> 

任何幫助將非常appreicated。

問候, Abnab

+0

你的問題不是很清楚 - 「爲各種事情傳遞參數」是什麼意思?你試圖通過什麼,從哪裏到哪裏,爲什麼你難以做到這一點? – IMSoP 2013-05-05 23:06:01

+0

@IMSoP感謝您的回覆。我試圖通過params變量傳遞請求。請求從Vend或物品明細獲取庫存。我在VEND API論壇中找到了這段代碼,但它並不清楚如何將值傳遞給它。 – abnab 2013-05-05 23:19:03

回答

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); 
+0

嗨, 感謝您的回答。可能是問題在我的結尾。我似乎沒有得到任何輸出。無論我發送get還是POST請求,都沒有輸出。但是,如果我直接將URL粘貼到瀏覽器上,則會出現結果。我正在從下面的鏈接中嘗試此代碼。 http://support.vendhq.com/entries/20954742-Simple-PHP-cURL-Example – abnab 2013-05-05 23:44:40

0

請檢查pyvend客戶端庫。