2015-05-18 63 views
0

想要確保我正確使用cURL以取代使用stream_context_create & file_get_contents的代碼段。cURL相當於file_get_contents&stream_context_create

這裏是從LinkedIn代碼示例原代碼:

$context = stream_context_create(
    array('http' => 
     array('method' => 'POST' //, ? unnecessary comma? 
     ) 
    ) 
); 

$response = file_get_contents($url, false, $context); 

這裏是替換代碼

function curl_get_contents($url) 
{ 
    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_URL, $url); 

    $data = curl_exec($ch); 
    curl_close($ch); 

    return $data; 
} 

$response = curl_get_contents($url); 

它看起來像原來的代碼是做一個帖子,不知道捲曲。替換cURL代碼會「工作」(而file_get_contents不會因爲allow_url_fopen問題)。但是,我想通過驗證替換代碼來避免將來的問題。我對cURL和PHP流媒體知之甚少。

謝謝...

+0

[tag:php]標籤應該被添加到這個問題中。 – SaidbakR

回答

0

添加curl_setopt($ch, CURLOPT_POST, 1);使捲曲發送HTTP POST請求。