php中使用curl,求助,多谢
在linux下使用下面命令可以执行:
curl -v -S -u devuser:devuser123 -F'notification={"applicationId":"5","schemaId":"12","topicId":"1","type":"USER"};type=application/json' -F file=@notification.json "http://135.252.37.213:8080/kaaAdmin/rest/api/sendNotification"
我在php中代码如下:
<?php
$ch = curl_init();
$user="devuser:devuser123";
curl_setopt($ch, CURLOPT_USERPWD,$user); //user:passwd
curl_setopt($ch,CURLOPT_URL,"http://135.252.37.213:8080/kaaAdmin/rest/api/sendNotification"); //url set
curl_setopt($ch, CURLOPT_VERBOSE, 1); //-v
curl_setopt($ch, CURLOPT_POST, 1); //-F 以表单方式
//post data
$post_data = array(
'notification' => array(
'applicationId' => '5',
'schemaId' => '12',
'topicId' => '1',
'type' => 'USER'
),
'type'=>'application/json',
'file'=>'@D:\WAMP\www\kaa\notification.json'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
$response = curl_exec($ch);
if($response === FALSE)
{
echo "cURL 具体出错信息: " . curl_error($ch);
}
curl_close($ch);
?>
返回结果如下:
HTTP ERROR 415
Problem accessing /kaaAdmin/rest/api/sendNotification. Reason:
Unsupported Media Type
请高手帮忙看看,多谢!