Jump to content


Inu's Content

There have been 1 items by Inu (Search limited from 29/03/2023)


Ordernar por                Order  

#1021163 Fopen Para Curl

Posted by Inu on 23/12/2014, 11:52 in PHP

Boa tarde,

Estou adaptando o código de um sistema que uso, tendo em vista que no meu ambiente de testes eu tenho o fopen() ativado, porém no ambiente de hospedagem que utilizo (produção), essa função é desativada por segurança.

No código, tenho a seguinte função:

    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;
    }

Eu alterei-a para:

    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 . "");
    }
    }

O problema aqui é que obtenho o seguinte erro:

Notice: Array to string conversion in /path/classes/xmwsclient.class.php on line 133 API Error: No 'request' method was recieved

E pelo que andei pesquisando, o problema está aqui:

               curl_setopt($curl, CURLOPT_HTTPHEADER, $params);

O problema é que não sei como adaptar essa parte, tendo em vista que o array $params é criado assim:

    $params = array('http' => array(
        'method' => 'POST',
        'content' => $data
        ));
    if ($optional_headers !== null) {
        $params['http']['header'] = $optional_headers;
    }

E quando mando imprimir o array, obtenho o seguinte:

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

Obrigado,





IPB Skin By Virteq