利用curl访问web service接口代码如下:
- $apiUrl = ‘http://test.com/test‘;
- $accessToken = ‘123456789‘;
- $header = [‘Authorization: ‘ . $accessToken,‘Content-Type:application/json‘];
- $post = [‘sku‘=>[]];
-
- $jsonPost = json_encode($post);
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_URL, $apiUrl);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonPost);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- $handles = curl_exec($ch);
- curl_close($ch);
-
- $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接口
原文:http://www.cnblogs.com/bobo-lx/p/6830365.html