dongzhi4239 2015-07-29 19:42
浏览 43

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

I need to create a PHP json object like { 12345 } and post it through cURL, but my code:

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

returns a string "{"0":"15876285"}" representing an associative array.

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.
"); 
   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条回答 默认 最新

  • dongran1779 2015-07-31 15:37
    关注

    @Barmar, thanks to you response I finally got past my moment of confusion. 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. 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. 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. 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

    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;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥15 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接