2017-07-30 45 views
0

當我發送帖子請求時,有一些問題。 我用這個腳本執行頁面http://localname.local/test,頁面http://localname.local/directory/page.php得到一個json數據。curl或file_get_contents不起作用

$url = "http://localname.local/directory/page.php"; 

$post = [ 
    "key1" => "hello", 
    "key2" => 885, 
]; 

$options = array(
    'http' => array(
     'header' => "Content-type: application/x-www-form-urlencoded\r\n", 
     'method' => 'POST', 
     'content' => http_build_query($post), 
     'timeout' => 10 
    ) 
); 
$context = stream_context_create($options); 
$result = file_get_contents($url, false, $context); 


if ($result === false) { 
    // Handle error 
    return null; 
} 
else 
    return $result; 

但經過10個secondes,劇本得到的消息:

Warning: file_get_contents(http://localname.local/directory/page.php): failed to open stream: HTTP request failed! in D:\ ... \html\test.php on line X

的page.php文件作品,我可以送崗位要求與我的瀏覽器作爲客戶端,但PHP(或WAMP)可以」訪問或發送請求到自己的頁面。

我得到了PHP 7.1.7,Apache 2.4.23在wam 3.0.9上,選項allow_url_fopen打開。

編輯: 爲捲曲

public static function get_content($post) 
{ 
    $url = "http://localname.local/directory/page.php"; 

    $query = http_build_query($post); 
    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_URL, $url); 
    curl_setopt($curl, CURLOPT_POST, true); 
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10); // Tell cURL that it should only spend X seconds trying to connect to the URL in question. 
    curl_setopt($curl, CURLOPT_TIMEOUT, 10); // A given cURL operation should only take X seconds max. 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // returns data to function 
    curl_setopt($curl, CURLOPT_POSTFIELDS, $query); 
    $data = curl_exec($curl); 

    if (curl_errno($curl)) 
     throw new Exception(curl_error($curl)); 

    curl_close($curl); 

    return $data; 
} 

得到

Fatal error: Uncaught Exception: Operation timed out after 10000 milliseconds with 0 bytes received in D:\ ... \html\test.php on line X

Exception: Operation timed out after 10000 milliseconds with 0 bytes received

+0

「test.php」通常(甚至有時)是否需要超過5秒才能運行? – ceejayoz

+0

不,它運行速度很快。 WAMP配置爲30秒,但在控制服務器之前我必須等待太久。 :/ – Rodrigue

+0

這個問題只是一個訪問錯誤,我認爲。 WAMP有可能無法訪問自己的域名嗎?我在本地主機上運行這個文件 – Rodrigue

回答

0

真正的問題

  1. test.php使用session_start()
  2. test.php推出的page.php
  3. page.php一個POST request使用session_start()< < <凍結

我們不能用2屆PHP在同一時間。會話鎖定並創建無限循環。

謝謝大家幫我找到解決方案。

1

對於file_get_contents()函數打開網址,您必須啓用php.ini文件中設置allow_url_fopen

安全明智的我勸你實現你與cURL做什麼:

function get_content($url,$post){ 
    $query = http_build_query($post); 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $URL); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // returns data to function 
    curl_setopt($ch, CURLOPT_POSTFIELDS,$query); 
    $data = curl_exec($ch); 
    // if you want to display error messages, do it before curl_close() 
    //echo curl_errno($ch); // if you want to display error number 
    //echo curl_error($ch); // if you want to display error message 
    curl_close($ch); 
    return $data; 
} 

echo get_content('http://localname.local/directory/page.php'); 
+0

Get'Warning:curl_errno():提供的資源不是位於D行的D:\ ... \ html \ test.php中的有效cURL句柄資源「 – Rodrigue

+1

這個錯誤是因爲你使用' 'curl_close()'後面的curl_errno()'你需要在'curl_close($ curl)'之前保留任何錯誤處理。 – Yolo

+0

哦,是的,對不起。謝謝,但現在的錯誤是'致命的錯誤:未捕獲異常:操作超時10000毫秒後收到0字節D:\ ... \ html \ test.php在X線上但我認爲問題是拒絕溝通(他自己的域名)是可能的嗎? – Rodrigue

0

我認爲你需要增加CURLOPT_TIMEOUT時間

+0

不,很抱歉,無論是30秒後停止的PHP腳本,還是CURL查詢以CURLOPT_TIMEOUT時間停止。該腳本在localhost中很快 – Rodrigue