【问题标题】:How to use curl instead of url fopen? [closed]如何使用 curl 而不是 url fopen? [关闭]
【发布时间】:2012-12-04 07:42:59
【问题描述】:

我正在使用以下代码,但出于安全原因,我的服务器 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);
    }

【问题讨论】:

  • Start by reading this 然后在您有具体问题要问时回来,而不仅仅是“请为我写这个”

标签: php curl fopen


【解决方案1】:
                 $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);

这对我有用。

【讨论】:

    【解决方案2】:

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

    <?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);
    
    ?>
    

    【讨论】:

    • 这段代码有问题
    • Parse error: syntax error, unexpected T_PRIVATE on line 8 为什么要在类外显示私有函数?
    • @DainisAbols 因为它来自一个类,在这种情况下其余的都无关紧要,我忘了删除 private 部分。对此感到抱歉。
    猜你喜欢
    • 2015-03-31
    • 1970-01-01
    • 2012-01-22
    • 2018-02-26
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多