PHP cURL form field values always empty
PHP cURL form field values always empty
I tried to access sslforfree.com using PHP cURL but the response form field values are always empty. Here's my codes:
$currUserAgent = $_SERVER['HTTP_USER_AGENT'];
define('USERNAME', 'roberts');
define('PASSWORD', 'mypass101');
define('USER_AGENT', $currUserAgent);
define('CERT', getcwd() . "/cert/cacert.pem");
define('COOKIE_FILE', 'cookie.txt');
$url = "https://www.sslforfree.com/login";
$postValues = array(
'email' => USERNAME,
'password' => PASSWORD,
'a' => 'login'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postValues);
$response = curl_exec($ch);
//Check for errors!
if(curl_errno($ch))
throw new Exception("cURL Error: ".curl_error($ch));
return $response;
return $result;
$response
use
return $result
instead of return $response
– adkstar
Aug 22 at 8:25
return $result
return $response
no need to use
http_build_query()
. The option supports array as the 3rd parameter. Missing CURLOPT_RETURNTRANSFER
option as well. Please read the official PHP documentation.– Raptor
Aug 22 at 9:13
http_build_query()
CURLOPT_RETURNTRANSFER
Sorry guys I forgot to change the variable but anyway this codes still not passing the values to the form.
– Robert Roberts
Aug 22 at 9:50
Raptor - Even if I remove the http_build_query(), still not passing the values of $postValues
– Robert Roberts
Aug 22 at 9:52
1 Answer
1
use return $result
instead of return $response
b/c when you execute $result = curl_exec($sth)
the result of curl execution will be stored in $result
variable
return $result
return $response
$result = curl_exec($sth)
$result
also, you may need to addcurl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
in case curl is directly outputting the response rather than storing it in the variable.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
Atleast add an explanation as to why he should use
return $result
.– Lewis Browne
Aug 22 at 8:26
return $result
explanation added
– adkstar
Aug 22 at 8:28
Thats better, Spoonfeeding people the answer is good but they will never learn. Telling people how to find the answer for themselves is much better.
– Lewis Browne
Aug 22 at 8:30
yeah. totally agree with you
– adkstar
Aug 22 at 8:31
Sorry guys I forgot to change the variable but anyway this codes still not passing the values to the form.
– Robert Roberts
Aug 22 at 9:51
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Try changing the last line to
return $result;
. You don't have a variable named$response
as far as I can see.– Stuart Wagner
Aug 22 at 8:22