Maurício Giordano
Maurício Giordano

Reputation: 3276

cURL post not working PHP

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.

Upvotes: 6

Views: 29886

Answers (5)

Zeeshan
Zeeshan

Reputation: 525

$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..

Upvotes: 7

Emre
Emre

Reputation: 899

Try these:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_CAINFO, '/etc/pki/tls/cert.pem'); // path is valid for RHEL/CentOS

This makes sure the resource you're "curling" has a valid SSL certificate. It is not recommended to set "CURLOPT_SSL_VERIFYPEER" to false (0).

Upvotes: 2

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100195

make your post data as:

$data = array('codpes' => 'someLogin', 'senusu' => 'somePass', 'Submit' => '1');
$postData = "";
foreach( $data as $key => $val ) {
   $postData .=$key."=".$val."&";
}
$postData = rtrim($postData, "&");

and change:

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

to

curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

Upvotes: 6

Alain
Alain

Reputation: 36984

You're on a secure connection, why are you using :

curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);

Use instead :

curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);

Upvotes: 1

Hanky Panky
Hanky Panky

Reputation: 46900

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

Upvotes: 14

Related Questions