首页 > 代码库 > php curl 访问web service接口

php curl 访问web service接口

 利用curl访问web service接口代码如下:

  1.     $apiUrl = ‘http://test.com/test‘;
  2.     $accessToken = ‘123456789‘;
  3.     $header = [‘Authorization: ‘ . $accessToken,‘Content-Type:application/json‘];
  4.     $post = [‘sku‘=>[]];
  5.     
  6.     $jsonPost = json_encode($post);
  7.     $ch = curl_init();
  8.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  9.     curl_setopt($ch, CURLOPT_URL, $apiUrl);
  10.     curl_setopt($ch, CURLOPT_POST, true);
  11.     curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonPost);
  12.     curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  13.     $handles = curl_exec($ch);
  14.     curl_close($ch);
  15.     
  16.     $result = json_decode($handles,true);

运行报错:Parse error: syntax error, unexpected ‘[‘

将以上代码1、2行替换为以下两行即可解决问题,原因是我的PHP版本是5.3.3,php5.4以下版本是不支持以上1、2行的写法的。

    $header = Array(‘Authorization: ‘.$accessToken,‘Content-Type:application/json‘) ;
    $post = array(‘sku‘=>array());

php curl 访问web service接口