【问题标题】:Convert Curl to wp_remote_post bitaps api将 Curl 转换为 wp_remote_post bitaps api
【发布时间】:2017-12-26 09:13:17
【问题描述】:

我必须将此 curl 转换为 wp_remote_post。

 function post_api($url, $postfields) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $result = curl_exec($ch);
    return $result;
  }
$postfields = json_encode(array('redeemcode'=> $redeemcode));
$data = post_api("https://bitaps.com/api/get/redeemcode/info", $postfields);

我试过这个但是出错了..... Jason Decode 错误。

    $data = wp_remote_post( "https://bitaps.com/api/get/redeemcode/info", array(
            'method' => 'POST',
            'timeout' => 45,
            'redirection' => 5,
            'httpversion' => '1.0',
            'blocking' => true,
            'headers' => array(),
            'body' => $postfields,
            'cookies' => array()
            )
        );

【问题讨论】:

    标签: php wordpress curl


    【解决方案1】:

    作为 JSON 数据的 curl 响应和 wp_remote_post() 提供数组格式的数据。 所以使用错误函数来识别错误或积极响应,如果收到错误,则使用 wp_remote_retrieve_body()。这可能会奏效。

    例如

    if( is_wp_error( $data ) ) {
    	return false;
    }
    
    $body = wp_remote_retrieve_body( $data );
    
    $data = json_decode( $body );

    【讨论】:

      【解决方案2】:

      您必须使用正确的值传递 $redeemcode。

      您可以使用以下示例:

       $redeemcode = "BTCuoC8AGRbHjEss347c4KoQrdzqyJwDSdAxbjAoC4tyAbxTAhBuq";
      

      结果

      [body] => {"balance": 0, "paid_out": 0, "pending_balance": 0, "address": "17BcAXNkpVKCrAAVKDr4CXDHLhXv9gTVY7"}
      

      【讨论】:

        【解决方案3】:

        所以我现在得到了答案,这是我用来帮助其他人的所有代码。

                 function post_api($url, $postfields) {
                        $data = wp_remote_post( $url, array(
                            'method' => 'POST',
                            'timeout' => 45,
                            'redirection' => 5,
                            'httpversion' => '1.0',
                            'blocking' => true,
                            'headers' => array(),
                            'body' => $postfields,
                            'cookies' => array()
                            )
                        );
                            if( is_wp_error( $data ) ) {
                                return false;
                            }
        
                    $body = wp_remote_retrieve_body( $data );   
        
                    return $body;
                }
                $postfields = json_encode(array('redeemcode'=> $redeemcode));
                $data = post_api("https://bitaps.com/api/get/redeemcode/info", $postfields);
        

        【讨论】:

          猜你喜欢
          • 2021-08-09
          • 1970-01-01
          • 2021-10-05
          • 1970-01-01
          • 2017-12-03
          • 1970-01-01
          • 2015-03-11
          • 2021-10-08
          • 1970-01-01
          相关资源
          最近更新 更多