KodeFor.Me
KodeFor.Me

Reputation: 13511

PHP and remote URL call

I try to create a plugin for WordPress that require in some point of the code to send some information to my server. The plugin will be installed in several servers, with diferent configurations and capabilities.

What I like to do is to make that script to be able to communicate with my server (just to call a specific URL of my server) in any kind of PHP Configuration and server capabilities.

Here is my code:

ini_set("allow_url_fopen", 1);

$d = array('key1' => 'val1', 'key2' => 'val2');
$data = urlencode(serialize($d));

$f = fopen('http://www.my-site.ext/getdata/' . $data . '/', 'r');
if(!$f)
{
    $ch = curl_init();

    if($ch != false)
    {
        curl_setopt($ch, CURLOPT_URL, 'http://www.my-site.ext/getdata/' . $data . '/');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $contents = curl_exec ($ch);
        curl_close ($ch);
    }
    else
    {
        // IS THERE ANY OTHER METHOD TO TEST IN CASE
        // THE FOPEN AND THE CURL NOT WORK ?
    }
}
else
{
    fclose($f);
}

So the question is, Is there any other method can I try in case that fopen and cURL not work on specific host ?

Upvotes: 0

Views: 1461

Answers (2)

Mob
Mob

Reputation: 11098

You can use file_get_contents() since it still sends a get request. However it, fopen and fsockopen still require allow_url_fopen to be enabled.

If safe-mode is enabled on the server it wouldn't be possible for you to make any network transaction. Peradventure you're using getcontents use unset() after the call to free resources so it doesn't hinder performance.

Upvotes: 1

Geoffrey
Geoffrey

Reputation: 11353

You could to the request manually via fsockopen and write a basic HTTP 1.0 client to perform the request, but if fopen url wrappers are disabled you will probably find that fsockopen is also disabled.

Upvotes: 1

Related Questions