-4

我正在使用以下代码,但allow_url_fopen出于安全原因,我的服务器已关闭。如何使用 curl 做到这一点。

     $request = array(
                    'method' => $method,
                    'params' => $params,
                    'id' => $currentId
                    );
    $request = json_encode($request);
    $this->debug && $this->debug.='***** Request *****'."\n".$request."\n".'***** End Of request *****'."\n\n";

    // performs the HTTP POST
    $opts = array ('http' => array (
                        'method'  => 'POST',
                        'header'  => 'Content-type: application/json',
                        'content' => $request
                        ));
    $context  = stream_context_create($opts);
    if ($fp = fopen($this->url, 'r', false, $context)) {
        $response = '';
        while($row = fgets($fp)) {
            $response.= trim($row)."\n";
        }
        $this->debug && $this->debug.='***** Server response *****'."\n".$response.'***** End of server response *****'."\n";
        $response = json_decode($response,true);
    } else {
        throw new Exception('Unable to connect to '.$this->url);
    }
4

2 回答 2

0

我为此目的编写了一个函数,它可能会对您有所帮助。不过要小心,因为它没有根据您的特定用例进行调整。

<?php
/**
 * @param $base     First part of the URL to direct the request to
 * @param $path     Second part of the URL
 * @param $param    Array of parameters to be used for the request
 * @return          The result of the request if successful otherwise false
 */
function request($base = "", $path = "", $param = array()) {
    $ch = curl_init();
    $url = $base . $path;
    if (isset($param["get"])) {
        $getparams = implode("&", $param["get"]);
        $url .= "?" . $getparams;
    }
    if (isset($param["post"])) {
        $postparams = implode("&", $param["post"]);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postparams);
    }
    if (isset($param["header"])) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $param["header"]);
    }
    curl_setopt_array($ch, array(
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_URL => $url
    ));
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}

$result = request("http://www.iana.org/domains/example/");
var_dump($result);

?>
于 2012-12-17T08:07:47.360 回答
0
                 $request = array(
                    'method' => $method,
                    'params' => $params,
                    'id' => $currentId
                    );
    $request = json_encode($request);
    $this->debug && $this->debug.='***** Request *****'."\n".$request."\n".'***** End Of request *****'."\n\n";

    // performs the HTTP POST

      $ch = curl_init($this->url);
            curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
            $response = json_decode(curl_exec($ch),true);
            curl_close($ch);

这对我有用。

于 2012-12-17T08:48:09.093 回答