Curl PHP login url post 302

Asked

Viewed 388 times

1

I’m having trouble authenticating on a system and using the component

live http header in Mozilla

i can handle know the url of the post and with Curl can automate see return image of live http inserir a descrição da imagem aqui

and my code I did so

    $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $urlPost);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:21.0) Gecko/20100101 Firefox/21.0');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); #Json
curl_setopt($ch, CURLOPT_ENCODING, ''); #Json
$data = curl_exec($ch);

plus the url of the post I had that cover is giving Location and in Curl can not complete the post , someone can help me and guides how I should do to it works ?

  • Good evening, I wonder if the answer helped you, if not please comment on what you think is missing.

1 answer

2

Actually it is going "perfect", the problem is that you have not enabled redirection, for this use:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

Use this way:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $urlPost);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:21.0) Gecko/20100101 Firefox/21.0');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); #Json
curl_setopt($ch, CURLOPT_ENCODING, ''); #Json
$data = curl_exec($ch);

Browser other questions tagged

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