简体   繁体   English

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

[英]How do I create a PHP json object with a single integer value?

I need to create a PHP json object like { 12345 } and post it through cURL, but my code:我需要创建一个像{ 12345 }这样的 PHP json 对象并通过 cURL 发布它,但是我的代码:

$json = json_encode(array($CaseID), JSON_FORCE_OBJECT);

returns a string "{"0":"15876285"}" representing an associative array.返回表示关联数组的字符串"{"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;    }

@Barmar, thanks to you response I finally got past my moment of confusion. @Barmar,多亏了你的回应,我终于摆脱了困惑的时刻。 Let me explain – I was reading the endpoint code line example on SIGNIFYD API docs and, between PHP, JSON and SIGNIFYD API, I got confused with {CASE_ID} notation.让我解释一下——我正在阅读 SIGNIFYD API 文档上的端点代码行示例,并且在 PHP、JSON 和 SIGNIFYD API 之间,我对 {CASE_ID} 表示法感到困惑。 Because the initial API call (Create Case) was a POST with JSON POSTFIELDS, I mistakenly assumed {CASE_ID} notation and a PHP encoded JSON with a single integer.因为最初的 API 调用(创建案例)是一个带有 JSON POSTFIELDS 的 POST,我错误地假设了 {CASE_ID} 表示法和一个带有单个整数的 PHP 编码的 JSON。 Truth be told I didn't pay much attention to the API docs where this (Retrieve Case) call is a GET and the https://api.signifyd.com/v2/cases/ {CASE_ID} endpoint should have the CASE_ID without the curly-brackets – in other words, this API call only receives a JSON response but, does not send any JSON at all.说实话,我并没有太注意 API 文档,其中这个(检索案例)调用是一个 GET 并且https://api.signifyd.com/v2/cases/ {CASE_ID} 端点应该有 CASE_ID 没有花括号——换句话说,这个 API 调用只接收一个 JSON 响应,但根本不发送任何 JSON。 Silly me!傻我!

This is the working version of the method.这是该方法的工作版本。 You also can see the complete class in https://github.com/mdutra555/SIGNIFYD_API_OpenCart_Model您还可以在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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM