将php url中的数据发布为json public function create\u plan\u paystack(){ $data=数组( “姓名”=>“每月聘用费”、“间隔”=>“每月”、“金额”=>500、'currency'=>“NGN'、'description'=>“测试” ); $sendjson=array(); $opts=数组( “http'=>数组( 'header'=>[“Authorization:Bearer”。$this->config->item('paystack_sk')。“\r\n”,“内容类型:application/json\r\n”], '方法'=>'获取', 'content'=>json\u encode($data)//http\u build\u query($data), ) ); 打印(可选); $context=stream\u context\u create($opts); $json=文件\u获取\u内容(“https://api.paystack.co/plan/“‘PLN_lp20ulqhzezihrc’,假,$context); $obj=json_decode($json); 印刷费($obj); }
我想把将php url中的数据发布为json public function create\u plan\u paystack(){ $data=数组( “姓名”=>“每月聘用费”、“间隔”=>“每月”、“金额”=>500、'currency'=>“NGN'、'description'=>“测试” ); $sendjson=array(); $opts=数组( “http'=>数组( 'header'=>[“Authorization:Bearer”。$this->config->item('paystack_sk')。“\r\n”,“内容类型:application/json\r\n”], '方法'=>'获取', 'content'=>json\u encode($data)//http\u build\u query($data), ) ); 打印(可选); $context=stream\u context\u create($opts); $json=文件\u获取\u内容(“https://api.paystack.co/plan/“‘PLN_lp20ulqhzezihrc’,假,$context); $obj=json_decode($json); 印刷费($obj); },php,json,codeigniter,Php,Json,Codeigniter,我想把$data数组作为json发布。 “这就是他们给我的方式。请帮帮我。使用curl $data = array("name" => "Hagrid", "age" => "36"); $data_string = json_encode($data);
$data
数组作为json发布。
“这就是他们给我的方式。请帮帮我。使用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);
或者你可以使用这两种方法中的任何一种
'method'=>'POST',我将方法更改为POST