Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cURL post not working PHP

Tags:

post

php

curl

I'm having trouble using cURL for a specific page.

A live code working: http://svgen.com/jupiter.php

Here is my code:

    $url = 'https://uspdigital.usp.br/jupiterweb/autenticar';

    $data = array('codpes' => 'someLogin', 'senusu' => 'somePass', 'Submit' => '1');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_COOKIEJAR, "FileHere");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "FileHere");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
    curl_exec($ch);
    curl_close($ch);

Although I had used the same url and post data, file_get_contents worked:

    $options = array('http' => array('method'  => 'POST','content' => http_build_query($data)));
    $context  = stream_context_create($options);
    $result = file_get_contents($url, false, $context);

    var_dump($result); 

Someone could help me? Thanks.

like image 818
Maurício Giordano Avatar asked Mar 01 '13 06:03

Maurício Giordano


2 Answers

Most probably it is the SSL verification problem.

Add

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

Also if you use CURLOPT_PROTOCOLS option, it should be HTTPS since you are posting to a secure url

curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);    // you currently have http
like image 123
Hanky Panky Avatar answered Nov 11 '22 00:11

Hanky Panky


$data = array('codpes' => 'someLogin', 'senusu' => 'somePass', 'Submit' => '1');

should have been

$data = http_build_query(array('codpes' => 'someLogin', 'senusu' => 'somePass', 'Submit' => '1'));

It automatically url encodes your query string as well and is safer than manual methods..

like image 7
Zeeshan Avatar answered Nov 10 '22 22:11

Zeeshan