php curl post body,PHP cURL POST返回415 – 不支持的媒体类型

我有一个简单的PHP脚本通过cURL发送HTTP POST请求,并期望响应中的json字符串(本来喜欢使用像pecl_http / HTTPRequest这样的现有库,但不能).调用始终失败,出现415错误 – 不支持的媒体类型.我想我没有正确配置cURL,但经过多次搜索,我无法找出我做错了什么.这是一些代码:

class URLRequest

{

public $url;

public $headers;

public $params;

public $body;

public $expectedFormat;

public $method;

public function URLRequest($aUrl, array $aHeaders, array $aParams, $aFormat = "json", $isPost = false, $aBody = "+")

{

$this->url = $aUrl;

$this->headers = $aHeaders;

$this->params = $aParams;

$this->expectedFormat = $aFormat;

$this->method = ($isPost ? "POST" : "GET");

$this->body = $aBody;

}

public function exec()

{

$queryStr = "?";

foreach($this->params as $key=>$val)

$queryStr .= $key . "=" . $val . "&";

//trim the last '&'

$queryStr = rtrim($queryStr, "&");

$url = $this->url . $queryStr;

$request = curl_init();

curl_setopt($request, CURLOPT_URL, $url);

curl_setopt($request, CURLOPT_HEADER, 1);

curl_setopt($request, CURLOPT_HTTPHEADER, $this->headers);

curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);

//curl_setopt($request, CURLOPT_SSL_VERIFYPEER, false);

if($this->method == "POST")

{

curl_setopt($request, CURLOPT_POST, 1);

curl_setopt($request, CURLOPT_POSTFIELDS, $this->body);

//this prevents an additions code 100 from getting returned

//found it in some forum - seems kind of hacky

curl_setopt($request, CURLOPT_HTTPHEADER, array("Expect:"));

}

$response = curl_exec($request);

curl_close($request);

preg_match("%(?<=HTTP/[0-9]\.[0-9] )[0-9]+%", $response, $code);

$resp = "";

if($this->expectedFormat == "json")

{

//parse response

}

elseif($this->expectedFormat == "xml")

{

//parse response

}

return $resp;

}

}

$url = "http://mydomain.com/myrestcall";

$query = array( "arg_1" => "test001",

"arg_2" => "test002",

"arg_3" => "test003");

$headers = array( "Accept-Encoding" => "gzip",

"Content-Type" => "application/json",

"Accept" => "application/json",

"custom_header_1" => "test011",

"custom_header_2" => "test012",

"custom_header_3" => "test013");

$body = array( "body_arg_1" => "test021",

"body_arg_2" => array("test022", "test023"),

"body_arg_3" => "test024");

$request = new URLRequest($url, $headers, $query, "json", true, $body);

$response = $request->exec();

……和回应:

HTTP/1.1 415 Unsupported Media Type

Server: Apache-Coyote/1.1

X-Powered-By: Servlet 2.5; JBoss-5.0/JBossWeb-2.1

Content-Type: text/html;charset=utf-8

Content-Length: 1047

Date: Mon, 18 Jun 2012 16:30:44 GMT

JBoss Web/2.1.3.GA - Error report

HTTP Status 415 -

type Status report

message

description The server refused this request because the request entity is in a format not supported by the requested resource for the requested method ().

JBoss Web/2.1.3.GA

任何见解或想法?

提前致谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值