如何使用单个整数值创建PHP json对象?

时间:2015-07-29 19:42:32

标签: php json curl

我需要创建一个像$json = json_encode(array($CaseID), JSON_FORCE_OBJECT); 这样的PHP json对象并通过cURL发布它,但是我的代码:

"{"0":"15876285"}"

返回表示关联数组的字符串public function RetrieveCase($CaseID) { $json = json_encode(array($CaseID), JSON_FORCE_OBJECT); //*** FOR DEBUG ONLY $f = fopen('sti_error.log', 'w'); $curl = curl_init($this->signifyd_api_endpoint); curl_setopt($curl, CURLOPT_HEADER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($json))); curl_setopt($curl, CURLOPT_USERPWD, $this->signifyd_trans_key . ":"); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $json); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Return response instead of printing. curl_setopt($curl, CURLOPT_TIMEOUT, 3); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3); curl_setopt($curl, CURLOPT_VERBOSE, 1); //** FOR DEBUG ONLY curl_setopt($curl, CURLOPT_STDERR, $f); curl_setopt($curl, CURLOPT_HEADER, 1); //** FOR DEBUG ONLY curl_setopt($curl, CURLINFO_HEADER_OUT, true); //** FOR DEBUG ONLY //** For debg only $fp = fopen ("sti_output.log", "w") or die("Unable to open stdout for writing.\n"); curl_setopt($curl, CURLOPT_FILE, $fp); $response = curl_exec($curl); if (!$response) { $this->session->data['error'] = 'SIGNIFYD RetrieveCase failed: ' . curl_error($curl) . '(#' . curl_errno($curl) . ')'; curl_close($curl); return false; } $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); fclose($f); //$this->load->language('payment/signifyd_transaction_investigation'); // HHTP Status Code 201 CREATED - The resource requested was successfully created. if ($httpcode != '201') { return $this->getCase($response); } $this->session->data['error'] = 'SIGNIFYD RetrieveCase failed response: ' . $response; return false; }

{{1}}

1 个答案:

答案 0 :(得分:0)

@Barmar,感谢你的回应我终于摆脱了困惑的时刻。让我解释一下 - 我正在阅读SIGNIFYD API文档上的端点代码行示例,在PHP,JSON和SIGNIFYD API之间,我对{CASE_ID}表示法感到困惑。因为初始API调用(Create Case)是带有JSON POSTFIELDS的POST,所以我错误地假定{CASE_ID}表示法和带有单个整数的PHP编码JSON。说实话,我没注意API文档,其中这个(检索案例)调用是GET而https://api.signifyd.com/v2/cases/ {CASE_ID}端点应该没有花括号的CASE_ID - 换句话说,这个API调用仅接收JSON响应,但根本不发送任何JSON。傻傻的我!

这是该方法的工作版本。您还可以在https://github.com/mdutra555/SIGNIFYD_API_OpenCart_Model

中查看完整的课程
public function RetrieveCase($CaseID) {

    //** From SIGNIFYD API Docs
    //        GET https://api.signifyd.com/v2/cases/{CASE_ID}    ****** beware of this {CASE_ID} notation, the {} curly-brackets must not be used.
    //   Plese note that "Retrieving a case" uses GET, unlike "Creating a new case" uses POST method.

    $url = $this->signifyd_api_endpoint . '/' . $CaseID;
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);    
    curl_setopt($curl, CURLOPT_HEADER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, 'Content-Type: application/json');
    curl_setopt($curl, CURLOPT_USERPWD, $this->signifyd_trans_key . ":");
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);   // Use 1 for curl_exec() to return the response header + content. DO NOT USE true boolean, it will not work.
    curl_setopt($curl, CURLOPT_TIMEOUT, 3);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3);

    $response = curl_exec($curl);
    if (!$response) {
        $this->session->data['error'] = 'SIGNIFYD RetrieveCase failed: ' . curl_error($curl) . '(#' . curl_errno($curl) . ')';
        curl_close($curl);
        return false;
    }

    $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);

    curl_close($curl);

    // HHTP Status Code 200 OK - The resource requested was successfully returned.                
    if ($httpcode == '200') {
        return $this->getCase($response);
    }
    $this->session->data['error'] = 'SIGNIFYD RetrieveCase failed response: ' . $response;
    return false;
}