2012-12-17 57 views
-4

我使用以下代碼,但出於安全原因,我的服務器allow_url_fopen處於關閉狀態。如何使用curl來做到這一點。如何使用curl而不是url fopen?

 $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

[開始閱讀此](http://php.net/curl)然後回來時,當你有一個具體的問題要問,而不只是一個「請給我寫這個」 – Dale

回答

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

這對我的作品。

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

?> 
+0

這個代碼有一個問題 – Dale

+0

'分析錯誤:語法錯誤,第8行出現意外的T_PRIVATE'爲什麼你要在類之外顯示私有函數? – Peon

+0

@DainisAbols因爲它是來自其中一個類別,其餘在這種情況下並不重要,我忘了刪除'private'部分。對於那個很抱歉。 – hielsnoppe