Curl https xml 结果在 PHP 中返回空白(布尔值 false)


Curl https xml result returns blank (boolean false) in PHP

请帮忙!我使用 cURL 发送帖子,但响应为空(布尔值为假)。如果我将数据发布到浏览器上,就像按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不是 http)的结果: 失败:错误号:60。原因:SSL 证书问题,请验证 CA 证书是否正常。 详细信息:错误:14090086:SSL 例程:SSL3_GET_SERVER_CERTIFICATE:证书验证失败
  • http不是https)的cURL结果只是重定向 www.example.com 的警告页面
  • var_dump($content); and var_dump(curl_getinfo($ch));结果是:

    布尔假数组(大小 = 26) 'url' => 字符串 'https://www.example.com/myaccount/transfer.php' (长度 = 46) "content_type" => 空 'http_code' => int 0 'header_size' => int 0 'request_size' => int 0 '文件时间' => int -1 'ssl_verify_result' => int 0 'redirect_count' => int 0 'total_time' =>浮点数 1.451 'namelookup_time' => 浮点数 0 'connect_time' => 浮点数 0 'pretransfer_time' => 浮点数 0 'size_upload' => 浮点数 0 'size_download' => 浮点数 0 'speed_download' => 浮点数 0 'speed_upload' => 浮点数 0 'download_content_length' => 浮点数 -1 'upload_content_length' => 浮点数 -1 'starttransfer_time' => 浮点数 0 'redirect_time' =>浮点数 0 '证书信息' => 数组(大小 = 0) 空 'primary_ip' => 字符串 '177.70.43.10' (长度 = 11) 'primary_port' => int 443 'local_ip' => 字符串 '192.168.11.9' (长度 = 0) 'local_port' => int 0 "redirect_url" =>字符串 '' (长度 = 0)

如果这个

SSL 证书

问题,请验证 CA 证书是否正常。 详细信息:错误: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);
将 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