【发布时间】:2014-12-23 18:24:33
【问题描述】:
我正在调整我使用的基于 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 . "");
}
}
这里的问题,是这个错误:
注意:数组到字符串的转换 /path/classes/xmwsclient.class.php 第 133 行 API 错误:没有“请求” 方法已收到
根据我的搜索,问题是因为:
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] => 数组 ( [方法] => POST [内容] => 0e0c97c0663f5db12a6ccfef0a513da3 获取设置 1))
有人可以帮助我吗? 谢谢!
【问题讨论】:
-
您的代码在当前状态下无法读取。请添加适当的换行符和缩进。
-
请正确识别您的代码à
-
@rafael 通过 phpbeautifier.com 修复了您的格式,请发布带有格式的代码
-
我试过了,但是当我发送到post时,系统返回错误代码。 :S
-
感谢 skrrgwasme!当我打开下一个主题或插入下一个代码时,我会试试这个! :)