【发布时间】:2014-08-11 00:35:48
【问题描述】:
我正在使用 PayPal 的经典 API,但在从 PHP 中的 curl_multi 获取结果时遇到了困难。我到目前为止的代码是:
<?php
$mh = curl_multi_init();
include('../connect.php');
$sql = "SELECT * FROM transactions ORDER BY ID asc LIMIT 0,2";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
$rid = $row['ID'];
$tid = $row['transID'];
$sandbox = FALSE;
// Set PayPal API version and credentials.
$api_version = '85.0';
$api_endpoint = 'https://api-3t.paypal.com/nvp';
$api_username = 'MY_USER';
$api_password = 'MY_PASS';
$api_signature = 'API_SIGNATURE';
$request_params = array
(
'USER' => $api_username,
'PWD' => $api_password,
'SIGNATURE' => $api_signature,
'VERSION' => $api_version,
'METHOD' => 'GetTransactionDetails',
'TRANSACTIONID' => $tid
);
$nvp_string = '';
foreach($request_params as $var=>$val)
{
$nvp_string .= '&'.$var.'='.urlencode($val);
}
${'ch_' . $i} = curl_init();
curl_setopt(${'ch_' . $i}, CURLOPT_VERBOSE, 1);
curl_setopt(${'ch_' . $i}, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt(${'ch_' . $i}, CURLOPT_TIMEOUT, 30);
curl_setopt(${'ch_' . $i}, CURLOPT_URL, $api_endpoint);
curl_setopt(${'ch_' . $i}, CURLOPT_RETURNTRANSFER, 1);
curl_setopt(${'ch_' . $i}, CURLOPT_POSTFIELDS, $nvp_string);
curl_multi_add_handle($mh, ${'ch_'.$i});
$i++;
}
}
include('../close.php');
$running = null;
do {
curl_multi_exec($mh, $running);
} while ($running);
?>
这已成功执行,通过注释掉 CURLOPT_RETURNTRANSFER 行来验证。我无法解决的是如何将返回的数据放入每个记录的数组中。我试过了:
$result = curl_multi_exec($mh, $running);
print_r($result);
这会返回一大堆 0 和 1,但不是我需要的细节。
有人可以帮忙吗?我的脑袋快要爆炸了……
【问题讨论】:
-
我可以看到您使用了 php 手册 (php.net/manual/en/function.curl-multi-add-handle.php) 中的示例。不幸的是,这样的代码效率低下,甚至是错误的。我建议使用
curl_multi包装器,例如:code.google.com/p/rolling-curl。或者这个(无耻的插头):github.com/hindmost/rolling-curl-mini