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);
Comments
-
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 over 11 years@MaurícioGiordano Sorry put 0 instead of false, my mistake
-
Maurício Giordano over 11 yearsIt tried both and not working... Note: file_get_contents use HTTP instead of HTTPS and works...
-
Alain Tiemblo over 11 yearsThen keep
HTTPS
, that's one error less :-)curl
andfile_get_contents
do not work the same way. -
Zeeshan almost 9 yearsWhy not use http_build_query() instead? Check my answer.
-
Richard over 8 yearsThanks 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 almost 7 yearsThis helps if your working curl-request strangely does not work inside your Laravel application