/    Sign up×
Community /Pin to ProfileBookmark

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:

[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;
}
[/code]

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 . “”);
}
}[/code]

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

[/quote]

And from what I’m searched, the problem is because:

[code] curl_setopt($curl, CURLOPT_HTTPHEADER, $params);[/code]

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;
}[/code]

And when I print this array, it’s what I obtain:

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

Someone can help me?
Thanks!

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @rafaht spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...