PHP curl_multi_exec输出到数组 [英] PHP curl_multi_exec output to array

查看:123
本文介绍了PHP curl_multi_exec输出到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PayPal的传统API,我无法在PHP中获取curl_multi的结果。我到目前为止的代码是:

I'm working with PayPal's classic API, and I'm having difficulty with getting the results from curl_multi in PHP. The code I have so far is:

<?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行来验证。我不能解决的是如何获取返回的数据到每个记录的数组。我试过:

This is successfully executing, verified by commenting out the CURLOPT_RETURNTRANSFER line. What I can't work out is how to get the returned data into an array for each record. I've tried:

$result = curl_multi_exec($mh, $running);
print_r($result);

这会返回一大堆0和1,但不是我需要的详细信息。

This returns a whole bunch of 0s and 1s, but not the details I need.

任何人都可以帮助?我的大脑即将爆炸...

Can anyone help? My brain is about to explode...

推荐答案

通过改变 $ {'ch_' 。$ i} 到数组 $ ch [$ i] ,然后使用 curl_multi_getcontent 如下:

Solved it by changing the ${'ch_'.$i} to an array $ch[$i] then looping through the array using curl_multi_getcontent as follows:

foreach ($ch as $a)
{
    $result = curl_multi_getcontent($a);
    parse_str($result, $arr);
    print_r($arr);
    echo "--------------------------------------------------\n";
}

这样就可以让数组中的数据 $ arr 。例如, $ arr ['EMAIL'];

This then lets me the data in the array $arr. For example, $arr['EMAIL'];

另请参阅:点击多次和返回转移理解php curl_multi_exec

这篇关于PHP curl_multi_exec输出到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆