我正在调整我使用的基于web的系统的代码,因为在我的开发环境中,我激活了fopen()函数,但是在生产环境中,出于安全原因,我没有激活这个函数。在代码中,我有一个函数:
function PostRequest($url, $data, $optional_headers = null)
{
$params = array(
'http' => array(
'method' => 'POST',
'content' => $data
)
);
if ($optional_headers !== null)
{
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp)
{
die("Problem reading data from " . $url . "");
}
$response = @stream_get_contents($fp);
// var_dump($response);
if ($response == false)
{
die("Problem reading data from " . $url . "");
}
return $response;
}我把它改成:
function PostRequest($url, $data, $optional_headers = null)
{
$params = array(
'http' => array(
'method' => 'POST',
'content' => $data
)
);
if ($optional_headers !== null)
{
$params['http']['header'] = $optional_headers;
}
// Customizations for fopen() or curl()
if (ini_get('allow_url_fopen') == true)
{
$params = array(
'http' => array(
'method' => 'POST',
'content' => $data
)
);
if ($optional_headers !== null)
{
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp)
{
die("Problem reading data from " . $url . "");
}
$response = @stream_get_contents($fp);
// var_dump($response);
if ($response == false)
{
die("Problem reading data from " . $url . "");
}
return $response;
}
else
if (function_exists('curl_init'))
{
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $params);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
else
{
die("Problem reading data from " . $url . "");
}
}这里的问题是这个错误:
注意:第133行API错误上的/path/classes/xmwsclient.class.php中的数组到字符串转换:没有收到“请求”方法。
据我所知,问题是:
curl_setopt($curl,CURLOPT_HTTPHEADER,$params);
但是我不知道如何调整这段代码,因为数组$params是尽快创建的:
<?php
$params = array(
'http' => array(
'method' => 'POST',
'content' => $data
)
);
if ($optional_headers !== null)
{
$params['http']['header'] = $optional_headers;
}当我打印这个数组时,我得到的是:
数组( http => Array (方法=> POST content => 0e0c97c0663f5db12a6ccfef0a513da3 GetSettings 1))
有人能帮我吗?谢谢!
发布于 2014-12-24 20:36:25
CURLOPT_HTTPHEADER不使用array[],这就是您在这里传递的内容。
$params = array(
'http' => array(
'method' => 'POST',
'content' => $data
)
);而且,这不是正确的使用它。这将是您的$optional_headers,必要时转换为string[]。要设置这些参数,可以使用CURLOPT_POST和CURLOPT_POSTFIELDS。
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);$data需要是包含原始数据的string[],或者是包含url编码数据的string。传递数组将设置Content-Type: multipart/form-data,而字符串将设置Content-Type: application/x-www-form-urlencoded。
有关详细信息,请参阅手册。
https://stackoverflow.com/questions/27625986
复制相似问题