Search code examples
phphttp-postfile-get-contents

Not working Using file_get_contents to POST data


I try to get content from https://www.freeformatter.com. I want to get POST data from that site. but my PHP script not working.

$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);

Solution

  • This will not worked. Reason is freeformatter.com is not allowing to give API or POST call. They always return you view-source of html page. This is allow to use from UI but not by API call. I tried with CURL post also. its gives return back view-source of html.

    Here is code you can try this works well with different

    
    <?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);
    
    

    Code doesnt have any issue. Issue is with URL.