无法使用 file_get_contents 至 POST 数据
Not working Using file_get_contents to POST data
我尝试从 https://www.freeformatter.com 获取内容。我想从该站点获取 POST 数据。但是我的 PHP 脚本不工作。
$data = array ('htmlString' => '<div class="container"><div class="row"><div class="col-md-8">&encoding=UTF-8', 'indentation' => 'THREE_SPACES');
$data = http_build_query($data);
$context_options = array (
'http' => array (
'method' => 'POST',
'header'=> "Content-type: application/x-www-form-urlencoded\r\n"
. "Content-Length: " . strlen($data) . "\r\n",
'content' => $data
)
);
$context = stream_context_create($context_options);
$result = file_get_contents('https://www.freeformatter.com/html-formatter.html', false, $context);
var_dump($result);
这行不通。原因是 freeformatter.com 不允许给 API 或 POST 打电话。他们总是 return 你查看 html 页面的来源。这允许从 UI 使用,但不能通过 API 调用。我也尝试过 CURL post 。它给出 return html 的后视图源。
这是您可以尝试使用不同
的代码
<?php
$data = array ('htmlString' => '<div class="container"><div class="row"><div class="col-md-8">&encoding=UTF-8', 'indentation' => 'THREE_SPACES');
$data = http_build_query($data);
$context_options = array (
'http' => array (
'method' => 'POST',
'header'=> "Content-type: application/x-www-form-urlencoded\r\n"
. "Content-Length: " . strlen($data['htmlString']) . "\r\n",
'content' => $data
)
);
$context = stream_context_create($context_options);
$result = file_get_contents('https://en99otyaenia.x.pipedream.net', false, $context);
var_dump($result);
代码没有任何问题。 URL 有问题。
我尝试从 https://www.freeformatter.com 获取内容。我想从该站点获取 POST 数据。但是我的 PHP 脚本不工作。
$data = array ('htmlString' => '<div class="container"><div class="row"><div class="col-md-8">&encoding=UTF-8', 'indentation' => 'THREE_SPACES');
$data = http_build_query($data);
$context_options = array (
'http' => array (
'method' => 'POST',
'header'=> "Content-type: application/x-www-form-urlencoded\r\n"
. "Content-Length: " . strlen($data) . "\r\n",
'content' => $data
)
);
$context = stream_context_create($context_options);
$result = file_get_contents('https://www.freeformatter.com/html-formatter.html', false, $context);
var_dump($result);
这行不通。原因是 freeformatter.com 不允许给 API 或 POST 打电话。他们总是 return 你查看 html 页面的来源。这允许从 UI 使用,但不能通过 API 调用。我也尝试过 CURL post 。它给出 return html 的后视图源。
这是您可以尝试使用不同
的代码
<?php
$data = array ('htmlString' => '<div class="container"><div class="row"><div class="col-md-8">&encoding=UTF-8', 'indentation' => 'THREE_SPACES');
$data = http_build_query($data);
$context_options = array (
'http' => array (
'method' => 'POST',
'header'=> "Content-type: application/x-www-form-urlencoded\r\n"
. "Content-Length: " . strlen($data['htmlString']) . "\r\n",
'content' => $data
)
);
$context = stream_context_create($context_options);
$result = file_get_contents('https://en99otyaenia.x.pipedream.net', false, $context);
var_dump($result);
代码没有任何问题。 URL 有问题。