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;





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


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 add
curl_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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)