Problem with file_get_contents

Asked

Viewed 383 times

-1

I’m trying to fetch data from a process (TRF site) with file_get_contents and nothing comes. When I paste the url into the browser, it comes. I’ve tried thousands of examples I’ve found on the Internet and nothing. I’ve tried the GET and POST methods and nothing. Follows my code:

$url = "https://processual.trf1.jus.br/consultaProcessual/arquivo/partes.php?proc=01303264620144019198&secao=TRF1&origem=juris";
$postdata = http_build_query(
    array(
        'proc' => '01303264620144019198',
        'secao' => 'TRF1',
        'origem' => $origem
    )
);
$opts = array('http' =>
    array(
        'method'=>'GET',
        'header' => array(
            'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*\/*;q=0.8',
            'Accept-Encoding: gzip, deflate, sdch',
            'Content-Type: text/html; charset=utf-8',
            'Content-type: application/x-www-form-urlencoded',
            'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Accept-Language:en-US,en;q=0.8',
            'Cache-Control:max-age=0',
            'Connection:keep-alive',
            'User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36',
            'Content-type: application/x-www-form-urlencoded'
        ),
        'content' => $postdata,
        'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*\/*;q=0.8',
        'User-Agent: '.$_SERVER['HTTP_USER_AGENT'],
        'request_fulluri' => True,
        'ignore_errors' => true,
    ),
    'ssl' => array(
        'verify_peer' => true,
        'ciphers' => 'HIGH:TLSv1.2:TLSv1.1:TLSv1.0:!SSLv3:!SSLv2',
        'disable_compression' => true
    )
);
$context  = stream_context_create($opts);
$dadosBrutos = file_get_contents($url, false, $context);
  • I copied your url, and pasted it in the browser and the message I got in response was "No part found for the searched process."

  • And right now I’m trying and it’s giving error in the url. As if trf1 had blocked the query or my ip.

  • Here returned a blank page,

2 answers

0

use a library called Guzzlehttpclient. follows the documentation:

http://docs.guzzlephp.org/en/stable/request-options.html

Example of how to get a get with her:

$client = new Client([
     'verify' => false //desativa o SSL
     'User-Agent' => Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36
]);

    $response = $client->request('GET', '/http://processual.trf1.jus.br/consultaProcessual/arquivo/partes.php', [
        'query'        => ['param' => '1'],

    ]);

        echo $response->getBody()->getContents();

query: are the get parameters of the URL: /test? param=1

link to download with:

https://packagist.org/packages/guzzlehttp/guzzle

If you don’t know, search on Composer, on how to install and use libraries she will make your life very easy.

has an error also in your url, you are passing it complete and then passing the parameters again below.

the url should look like this:

$url = "https://processual.trf1.jus.br/consultaProcessual/arquivo/partes.php

as you are using http_build_query, the parameters come after.

0

You can use Curl instead of file_get_contents.

Example:

$baseUrl = 'https://processual.trf1.jus.br/consultaProcessual/arquivo/partes.php';

$url = $baseUrl . '?' . http_build_query([
  'proc'   => '01303264620144019198',
  'secao'  => 'TRF1',
  'origem' => 'juris'
]);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_HEADER, false);

$response = curl_exec($ch);
curl_close($ch);

var_dump($response);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.