background image

3.2 Post 方式实现
代码如下:

 

   $url = "http://localhost/web_services.php";
  $post_data = array ("username" => "bob","key" => "12345");
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  // post 数据
  curl_setopt($ch, CURLOPT_POST, 1);
  // post 的变量
  curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  $output = curl_exec($ch);
  curl_close($ch);
  //打印获得的数据
  print_r($output);
  

  以上方式获取到的数据是 json 格式的,使用 json_decode 函数解释成数组。
  $output_array = json_decode($output,true);
  如果使用 json_decode($output)解析的话,将会得到 object 类型的数据。