无法使用file_get_contents发布数据

问题描述 投票:0回答:1

我尝试从https://www.freeformatter.com获取内容。我想从该site.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);
php http-post file-get-contents
1个回答
0
投票

这不起作用。原因是freeformatter.com不允许进行API或POST调用。他们总是返回您html页面的视图源。允许从UI使用,但不能通过API调用使用。我也尝试过CURL发布。它提供了返回html的view-source。

这里是代码,您可以尝试将其与其他代码配合使用


<?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。

© www.soinside.com 2019 - 2024. All rights reserved.