Jet API连接问题

时间:2016-02-07 21:30:18

标签: php json curl

到目前为止,我正在邮递员中连接到jet.com,但是当我尝试通过php连接时,我没有收到任何错误或响应。该页面是空白的。到目前为止,这是我的代码:

protected static $api_user_id = "****";
protected static $api_secret = "****";
protected static $merchant_id = "****";
protected static $api_prefix = "https://merchant-api.jet.com/api/";
class Jet
{
    public function getNewToken()
    {
    $ch = curl_init($this::$api_prefix.'/Token');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //If necessary add your ssl pem: curl_setopt($ch, CURLOPT_CAINFO,'/ssl/cacert.pem');
    $request = json_encode(array(
        "user" => $this::$api_user_id,
        "pass" => $this::$api_secret
    ));                                              
    curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/json',                                                                                
        'Content-Length: ' . strlen($request))                                                                       
    );       
    $data = curl_exec($ch);
    curl_close($ch);
    if($data = json_decode($data)){
        if($token = $data->id_token){
            //SAVE $token SOMEWHERE and save last time you got a token
            $this->setToken($token);
            $this->setTokenTs(date('r'));
            $this->save();
            echo $token;
            return true;
        }
    }
    return false;

    $return = curl_exec($ch);   
    $err = curl_error($ch);

    curl_close($ch);

    if ($err) 
    {
        echo "cURL Error #:" . $err;
    } 
    else 
    {
        echo $return;
    }
    }
}
$jetAPI = new Jet;
$jetAPI->getNewToken();

我不确定我做错了什么。任何帮助将不胜感激。感谢。

1 个答案:

答案 0 :(得分:1)

我正在使用:

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $obj);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: $app"));
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($curl);

此处的关键是curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);