cURL PHP login url post 302

1

I'm having trouble authenticating to a system and using the

  

live http header in mozilla

I get handle knows the url of the post and with curl it can automate see the live http return image

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

the url of the post that I had that cover is giving location and in curl can not complete the post, can someone help me and guide how should I do so that it works?

    
asked by anonymous 26.06.2015 / 02:13

1 answer

2

In fact it's going to be "perfect", the problem is that you did not enable redirection, so 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);
    
26.06.2015 / 03:49