PHP cURL表单字段值始终为空

时间:2018-08-22 08:20:30

标签: php php-curl

我尝试使用PHP cURL访问sslforfree.com,但响应表单字段值始终为空。这是我的代码:

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

1 个答案:

答案 0 :(得分:0)

使用return $result代替return $response b / c当您执行$result = curl_exec($sth)时,curl执行的结果将存储在$result变量中

另外,您可能需要添加 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 如果curl直接输出响应而不是将其存储在变量中。