1

我正在使用 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() 返回 url 时,它将像这样打印“ https://299-BYM-827.mktorest.com/rest/v1/leads.json?access_token= ”你会注意到我没有得到访问令牌。当我从 getToken() 打印 URl 时,它将打印正确的 url,当我将此 URL 输入浏览器时,我将得到正确的输出。谢谢 。

4

1 回答 1

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 URL,如果它正在更改某些内容,请检查您的 bodyBuilder()。

于 2016-03-22T10:54:07.503 回答