1

我尝试使用 PHP Curl 通过 post 方法提交值。但它不适用于 HTTPS 页面。任何人都会查看我的以下代码并检查我做错了什么。

$url = 'https://www.whoisxmlapi.com/user/create.php';
$fields = array(
                        'username' => urlencode("check123"),
                        'email' => urlencode("check123@gmail.com"),
                        'password' => urlencode("password"),
                        'password_confirmation' => urlencode("password"),
                        'firstname' => urlencode("firstname"),
                        'lastname' => urlencode("lastname"),
                        'organization' => urlencode("organization"),
                        'captcha' => urlencode("captcha")
                );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');


//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT ,3);
curl_setopt($ch,CURLOPT_TIMEOUT, 20);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);
4

2 回答 2

2

请首先在您的代码中添加一些有用的错误检查

//execute post
$result = curl_exec($ch);

if( $result === false) {
    echo 'Curl error: ' . curl_error($ch);
}

然后你可能能够解决你自己的问题

于 2015-08-08T15:29:10.333 回答
0

分享您遇到的错误,或使用此 curl 选项进行测试

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);

0:不检查公用名 (CN) 属性

1:检查公用名属性是否至少存在

2:检查公用名是否存在并且与服务器的主机名匹配

于 2015-08-08T15:29:33.503 回答