cURL post not working PHP

25,057

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

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

Solution 5

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);
Share:
25,057
Maurício Giordano
Author by

Maurício Giordano

Follow me on Twitter: @mauriciogior

Updated on August 08, 2022

Comments

  • Maurício Giordano
    Maurício Giordano almost 2 years

    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.

  • Hanky Panky
    Hanky Panky over 11 years
    @MaurícioGiordano Sorry put 0 instead of false, my mistake
  • Maurício Giordano
    Maurício Giordano over 11 years
    It tried both and not working... Note: file_get_contents use HTTP instead of HTTPS and works...
  • Alain Tiemblo
    Alain Tiemblo over 11 years
    Then keep HTTPS, that's one error less :-) curl and file_get_contents do not work the same way.
  • Zeeshan
    Zeeshan almost 9 years
    Why not use http_build_query() instead? Check my answer.
  • Richard
    Richard over 8 years
    Thanks for this-- for some reason http_build_query() worked for me on one server, but failed on another. Your solution helped me create a workaround that works on both servers.
  • Michael Käfer
    Michael Käfer almost 7 years
    This helps if your working curl-request strangely does not work inside your Laravel application