Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

billok-lab's avatar

HTTP Request using Guzzle

Hello guys, I need some help on my project, I have an http request using curl and I want to change it using guzzle

here is my code

public function create (){
    $client_id = '0987';
    $secret_key = 'fjkdsla;inj;hd898fdsre32re';
    $url = 'https://apibeta.bni-ecollection.com/';


    $data_asli = array(
        'client_id' => $client_id,
        'trx_id' => mt_rand(), // fill with Billing ID
        'trx_amount' => 300000,
        'billing_type' => 'c',
        'datetime_expired' => date('c', time() + 1200 * 3600), // billing will be expired in  50 days
        'virtual_account' => '',
        'customer_name' => 'Just an Example',
        'customer_email' => '[email protected]',
        'customer_phone' => '081268928342',
        'type' => 'createBilling'
    );

    $hashed_string = BniEnc::encrypt(
        $data_asli,
        $client_id,
        $secret_key
    );

    $data = array(
        'client_id' => $client_id,
        'data' => $hashed_string,
    );

    $response = get_content($url, json_encode($data));
    $response_json = json_decode($response, true);

    if ($response_json['status'] !== '000') {
        // handling jika gagal
        var_dump($response_json);
    }
    else {
        $data_response = BniEnc::decrypt($response_json['data'], $client_id, $secret_key);
        // $data_response will contains something like this:
        // array(
        // 	'virtual_account' => 'xxxxx',
        // 	'trx_id' => 'xxx',
        // );
        var_dump($data_response);
    }

    function get_content($url, $post = '') {
        $usecookie = __DIR__ . "/cookie.txt";
        $header[] = 'Content-Type: application/json';
        $header[] = "Accept-Encoding: gzip, deflate";
        $header[] = "Cache-Control: max-age=0";
        $header[] = "Connection: keep-alive";
        $header[] = "Accept-Language: en-US,en;q=0.8,id;q=0.6";

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_VERBOSE, false);
        // curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_ENCODING, true);
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
        curl_setopt($ch, CURLOPT_MAXREDIRS, 5);

        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36");

        if ($post)
        {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        }

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        $rs = curl_exec($ch);

        if(empty($rs)){
            var_dump($rs, curl_error($ch));
            curl_close($ch);
            return false;
        }
        curl_close($ch);
        return $rs;
    }

}

any help would be great

0 likes
1 reply

Please or to participate in this conversation.