2015-09-01 77 views
1

我使用的Marketo REST API.Here我寫這段代碼無法獲得訪問令牌的Marketo REST API

class UpsertLeads{ 
private $host = "";//CHANGE ME 
private $clientId = "";//CHANGE ME 
private $clientSecret = "";//CHANGE ME 
public $input; //an array of lead records as objects 
public $lookupField; //field used for deduplication 
public $action; //operation type, createOnly, updateOnly, createOrUpdate, createDuplicate 

public function postData(){ 
    $url = $this->host . "/rest/v1/leads.json?access_token=" . $this->getToken(); 
    $ch = curl_init($url); 
    $requestBody = $this->bodyBuilder(); 
    print_r($requestBody); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json','Content-Type: application/json')); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody); 
    curl_getinfo($ch); 
    $response = curl_exec($ch); 
    return $response; 
} 

private function getToken(){ 
    $ch = curl_init($this->host . "/identity/oauth/token?grant_type=client_credentials&client_id=" . $this->clientId . "&client_secret=" . $this->clientSecret); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json',)); 
    $response = json_decode(curl_exec($ch)); 
    curl_close($ch); 
    $token = $response->access_token; 
    return $token; 
} 

,當我從POSTDATA(返回地址),這將是打印這樣 「https://299-BYM-827.mktorest.com/rest/v1/leads.json?access_token=」 你可以注意到我沒有獲得訪問令牌。 當我從getToken()打印URl時,它將打印正確的URL,當我將這個URL打到瀏覽器中時,我將得到正確的輸出。 謝謝。

回答

1
public function postData(){ 
    $url = $this->host . "/rest/v1/leads.json?access_token=" . $this->getToken(); 
    $ch = curl_init($url); 
    //$requestBody = $this->bodyBuilder(); 
    //print_r($requestBody); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json','Content-Type: application/json')); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    //curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody); 
    curl_getinfo($ch); 
    $response = curl_exec($ch); 
    //echo"<pre>";print_r($response);exit(); 
    return $url; 
} 

正在返回具有有效訪問令牌的完美API網址,請檢查您的bodyBuilder()是否正在更改某些內容。