当我发送帖子请求时,有一些问题。 我用这个脚本执行页面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
“test.php”通常(甚至有时)是否需要超过5秒才能运行? – ceejayoz
不,它运行速度很快。 WAMP配置为30秒,但在控制服务器之前我必须等待太久。 :/ – Rodrigue
这个问题只是一个访问错误,我认为。 WAMP有可能无法访问自己的域名吗?我在本地主机上运行这个文件 – Rodrigue