fopen to cURL

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:

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; }
I changed-a to:

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:

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:

               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:

$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:

Array ( [http] => Array ( [method] => POST [content] => 0e0c97c0663f5db12a6ccfef0a513da3 GetSettings 1 ) )

Someone can help me?
Thanks!

Well, with a very quick review of your code, I think it is your if-clause logic.

You should have, in general terms, something loosely like this…

if (ini_get(‘allow_url_fopen’) == true) {
do some code for the url-allowed section…
} ELSE {
do other code for url-NOT-allwed section…
>>> in this group, handle the second set of compares <<<
if (function_exists(‘curl_init’)) {
do code if curl exists…
} ELSE {
do cod if curl does NOT exist…
}
}

You put your second routine into the else of the first section. The routines for the FP and curl versions in
separate areas. You combined them…

Hope that makes sense… And, hope you sort it out…

Sponsor our Newsletter | Privacy Policy | Terms of Service