首页 > 代码库 > php curl 访问web service接口
php curl 访问web service接口
利用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接口
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。