开发者

What's the best way to POST to a web API with PHP?

I've开发者_如何学C seen curl() used as a way to POST - are there other ways that are more widely used, or better?


Seeing the incredibly huge amount of settings you have with cURL, there's probably no reason to use anything else.


As of PHP 4.3 and 5, You can also use stream_context_create() in conjuction with fopen() / file_get_contents() to make POST requests.

A full POST example is here.

As for which is better, I have never seen a PHP install with cURL support not compiled in. But seeing as it needs an external library, and the stream context method does not, one could argue that the latter is the better choice for portable applications.

CURL is still the more flexible of tools, and has more options and functions. But if it's just POST requests you need to make, I would use the built-in way.


AFAIK, cURL is the recommended way for PHP to POST to another API. There may be other ways of doing it, but cURL is built-in to PHP to handle situations just like this, so why not use it?


I answered a similar question recently, that provides a basic POST'able implementation of both file_get_contents() and cURL and some benchmarks which should help you decide.

It was already mentioned that cURL requires the libcurl extension, and on some servers file_get_contents() might not able to request remote files is allow_url_fopen is set to Off.

You'll have to choose which one works best for you, I normally use the following function which falls back to file_get_contents() if cURL is not available.

function Request($url, $post = null)
{
    if (extension_loaded('curl') === true)
    {
        $curl = curl_init($url);

        if (is_resource($curl) === true)
        {
            curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
            curl_setopt($curl, CURLOPT_FAILONERROR, true);
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

            if (isset($post) === true)
            {
                curl_setopt($curl, CURLOPT_POST, true);
                curl_setopt($curl, CURLOPT_POSTFIELDS, (is_array($post) === true) ? http_build_query($post, '', '&') : $post);
            }

            $result = curl_exec($curl);
        }

        curl_close($curl);
    }

    else
    {
        $http = array
        (
            'method' => 'GET',
            'user_agent' => $_SERVER['HTTP_USER_AGENT'],
        );

        if (isset($post) === true)
        {
            $http['method'] = 'POST';
            $http['header'] = 'Content-Type: application/x-www-form-urlencoded';
            $http['content'] = (is_array($post) === true) ? http_build_query($post, '', '&') : $post;
        }

        $result = @file_get_contents($url, false, stream_context_create(array('http' => $http)));
    }

    return $result;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜