Hello,
I’m adapting the code of a web based system I use, given that on my development ambient I have the fopen() function activated, but on the production ambient, I haven’t this function active, for security reasons.
On the code, I have this function:
[code] 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;
}
die(“Problem reading data from ” . $url . “”);
}
return $response;
}
I changed to:
[code] 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 . “”);
}
}
The problem in here, is this error:
[quote]
Notice: Array to string conversion in /path/classes/xmwsclient.class.php on line 133 API Error: No ‘request’ method was recieved
And from what I’m searched, the problem is because:
[code] curl_setopt($curl, CURLOPT_HTTPHEADER, $params);
But I don’t know how to adapt this piece of code, because the array $params is created as soon:
[code] $params = array(‘http’ => array(
‘method’ => ‘POST’,
‘content’ => $data
));
if ($optional_headers !== null) {
$params[‘http’][‘header’] = $optional_headers;
}
And when I print this array, it’s what I obtain:
[code]Array ( [http] => Array ( [method] => POST [content] => 0e0c97c0663f5db12a6ccfef0a513da3 GetSettings 1 ) )
Someone can help me?
Thanks!