Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Laracast13's avatar

Using Curl to login into website and get data

Hello Website have json data which is accessible only for login user.

Using Curl I have successfully login

$cookie="cookie.txt";
$data = [ "password" => 'test', "username" => '1',];
$data_json = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.site.com/Login');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response  = curl_exec($ch);
curl_close($ch);
echo $response;

but how can I read json data from protected url

0 likes
6 replies
jlrdw's avatar

Is it your site? If 3rd party they will have instructions.

jlrdw's avatar

@www888 any api that allows access should have some basic usage instructions.

Laracast13's avatar

@jlrdw This not API and you don't understand what I am asking

This is for ex. Login with curl to amazaon.com login page and then access order page

Please or to participate in this conversation.