将Curl转换为wp_remote_post bitaps api

问题描述 投票:0回答:3

我必须将此卷曲转换为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);

我尝试了这个,但得到错误..... Json 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
3个回答
1
投票

卷曲响应作为JSON数据&wp_remote_post()给出数组格式化数据。因此,使用错误函数来识别错误或肯定响应,如果收到错误,则使用wp_remote_retrieve_body()。这可能会奏效。

防爆。

if( is_wp_error( $data ) ) {
	return false;
}

$body = wp_remote_retrieve_body( $data );

$data = json_decode( $body );

0
投票

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

您可以使用以下示例:

 $redeemcode = "BTCuoC8AGRbHjEss347c4KoQrdzqyJwDSdAxbjAoC4tyAbxTAhBuq";

结果:

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

0
投票

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

         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);
© www.soinside.com 2019 - 2024. All rights reserved.