1

请需要你的帮助!我使用 cURL 发送帖子,但响应为空白(布尔值 false)。如果我像按浏览器一样在浏览器上发布数据,https://www.example.com/myaccount/transfer.php?username=usernamer&password=pass&deposit=5则会显示以下结果:

1 0 success Your account has been deposited.

当我看到浏览器源代码时,结果是:

<?xml version="1.0" encoding="utf-8"?>
<TransferResponse>
<version>1</version>
<result>0</result>
<resultstring>success</resultstring>
<description>Your account has been deposited</description>
</TransferResponse>

让我附上我的 cURL 代码:

<?php

$config = array
(
"url"        => "https://www.example.com/myaccount/",
"transfer"   => "transfer.php",
"browser"    => "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16"
);

$postFields = "username=username&password=pass&deposit=5";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $config['url'] . $config['transfer']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/cert/GeoTrustGlobalCA.crt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $config['browser']);
curl_setopt($ch, CURLOPT_REFERER, $config['url']);
curl_setopt($ch, CURLOPT_HEADER, true);

$content = curl_exec($ch);

var_dump($content);
var_dump(curl_getinfo($ch));
curl_error($ch);

curl_close($ch);

echo $content;

?>

请帮助我在 cURL 中获得结果。我尝试了很多方法。我已经连续上网 2 天了。最后我决定在这里问。有人可以找出问题所在吗?执行以下操作:

  • 命令行将http (不是 https)的结果显示为 xml。
  • 命令行返回https (not http)的结果如下: Failed: Error Number: 60. Reason: SSL证书问题,验证CA证书是否OK。详细信息:错误:14090086:SSL 例程:SSL3_GET_SERVER_CERTIFICATE:证书验证失败
  • http (不是 https)的 cURL 结果只是重定向 www.example.com 的警告页面
  • var_dump($content); and var_dump(curl_getinfo($ch));结果是:

    boolean false array (size=26) 'url' => string ' https://www.example.com/myaccount/transfer.php' (length=46) 'content_type' => null 'http_code' => int 0 'header_size' => int 0 'request_size' => int 0 'filetime' => int -1 'ssl_verify_result' => int 0 'redirect_count ' => int 0 'total_time' => float 1.451 'namelookup_time' => float 0 'connect_time' => float 0 'pretransfer_time' => float 0 'size_upload' => float 0 'size_download' => float 0 'speed_download' => 浮动 0 'speed_upload' => 浮动 0 'download_content_length' => 浮动 -1 'upload_content_length' => 浮动 -1 'starttransfer_time' => 浮动 0 'redirect_time' => 浮动 0 'certinfo' => 数组(大小= 0) 空'primary_ip' => string '177.70.43.10' (length=11) 'primary_port' => int 443 'local_ip' => string '192.168.11.9' (length=0) 'local_port' => int 0 'redirect_url' =>字符串''(长度=0)

4

2 回答 2

1

将 SSL 验证设置为 false 会破坏 SSL 的目的。一个合适的解决方案是为 CURL 设置 CA 证书。

解决方案 1(更改 PHP.ini):

  1. 从http://curl.haxx.se/docs/caextract.html下载 CA 包 (cacert.pem)
  2. 将它放在您的本地系统上(例如:C:\xampp\cacert.pem)
  3. 打开你的 php.ini
  4. 将 curl.ca_info 选项设置为指向 cacert.pem 的位置

    Example: curl.ca_info="C:\xampp\cacert.pem"
    
  5. 重启阿帕奇

解决方案 2(在每次 CURL 调用之前设置选项)

  1. 从http://curl.haxx.se/docs/caextract.html下载 CA 包 (cacert.pem)
  2. 将它放在您的本地系统上(例如:C:\xampp\cacert.pem)
  3. 编写以下代码:

    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, TRUE); 
    curl_setopt ($ch, CURLOPT_CAINFO, "pathto\cacert.pem");
    

来源:http ://tumblr.wehavefaces.net/post/52114563111/environment-windows-xampp-curl-library

于 2013-06-04T04:00:31.450 回答
1

如果这

SSL证书问题,验证CA证书是否OK。详细信息:错误:14090086:SSL 例程:SSL3_GET_SERVER_CERTIFICATE:证书验证失败

是你的问题,curl_setopt()设置

CURLOPT_SSL_VERIFYPEER

可能适合你:

FALSE 阻止 cURL 验证对等方的证书。可以使用 CURLOPT_CAINFO 选项指定要验证的备用证书,或者可以使用 CURLOPT_CAPATH 选项指定证书目录。

所以设置

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

可能还有

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
于 2013-01-29T19:53:38.977 回答