将数据作为json发布在php url中

时间:2016-12-27 05:14:29

标签: php json codeigniter

public function create_plan_paystack(){
    $data = array(
        "name"=> "Monthly retainer", "interval"=>"monthly", "amount"=>500,'currency'=>'NGN','description'=>'test'
    );

    $sendjson = array();
    $opts = array(
        'http'=>array(
            'header'=> ["Authorization: Bearer ".$this->config->item('paystack_sk')."\r\n" ,"Content-Type: application/json\r\n"] ,
            'method'  => 'GET',
            'content' => json_encode($data)// http_build_query($data),
        )
    );
    print_r($opts);
    $context = stream_context_create($opts);
    $json = file_get_contents("https://api.paystack.co/plan/".'PLN_lp20ulqhzezihrc',false,$context);

    $obj = json_decode($json);
    print_r($obj);
}

我想将$data数组发布为json。 " https://developers.paystack.co/docs/create-plan"这就是他们给予的方式.plaese帮助我。

1 个答案:

答案 0 :(得分:0)

使用curl

    $data = array("name" => "Hagrid", "age" => "36");                                                                    
    $data_string = json_encode($data);                                                                                   

    $ch = curl_init('http://api.local/rest/users');                                                                      
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/json',                                                                                
        'Content-Length: ' . strlen($data_string))                                                                       
    );                                                                                                                   

    $result = curl_exec($ch);

或者您可以使用其中任何一种

https://github.com/yabacon/paystack-php

https://github.com/MalikAbiola/paystack-php-lib

相关问题