猿问

不工作使用 file_get_contents 发布数据

我尝试从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);


牧羊人nacy
浏览 103回答 1
1回答

慕后森

这行不通。原因是 freeformatter.com 不允许提供 API 或 POST 调用。他们总是返回你的 html 页面的视图源。这允许从 UI 使用,但不能通过 API 调用。我也尝试过 CURL 帖子。它返回html的视图源。这是您可以尝试的代码,它适用于不同的<?php$data = array ('htmlString' => '<div class="container"><div class="row"><div class="col-md-8">&encoding=UTF-8', 'indentation' => 'THREE_SPACES');&nbsp;$data = http_build_query($data);$context_options = array (&nbsp; &nbsp; &nbsp; &nbsp; 'http' => array (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'method' => 'POST',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'header'=> "Content-type: application/x-www-form-urlencoded\r\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; . "Content-Length: " . strlen($data['htmlString']) . "\r\n",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'content' => $data&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; );$context = stream_context_create($context_options);&nbsp;$result = file_get_contents('https://en99otyaenia.x.pipedream.net', false, $context);&nbsp;var_dump($result);代码没有任何问题。问题在于 URL。
随时随地看视频慕课网APP
我要回答