PHP,卷曲.curl_exec 返回什么? [英] PHP, CURL. What does curl_exec return?

查看:24
本文介绍了PHP,卷曲.curl_exec 返回什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用支付处理器设置 API.下面是他们提供给我的代码.在 $result 变量中有一些我想要的信息,我不明白的是什么类型的变量是 '$result' 以及如何从中获取某些数据.打印 $result 显示交易 ID 为:xxxx 状态为接受".我基本上想要的是只获取交易 ID 并将其存储在一个变量中.

I'm trying to set an API with a payment processor. Below is the code they provided me. There are some information in the $result variable that I want, what I don't understand is what type of variable is '$result' and how can I take certain data from it. printing the $result shows "Transaction ID is : xxxx status is ACCEPTED". What I basicly want is to take only the transaction ID and store it in a variable.

foreach($_POST as $k=>$v) $$k=urldecode($v); 
$urladdress = "https://example.com/accapi/process.php"; 
$api_id = "dddd"; 
$api_pwd = "yyyyy"; 
$api_pwd = md5($api_pwd.'s+E_a*'); 
$data = "user=".$user. "&testmode=".$testmode."&api_id=".$api_id. "&api_pwd=".$api_pwd."&amount=".$amount."&paycurrency=".$currency."&comments=".$comments."&fee=".$fee."&udf1=".$udf1;
// Call STP API

$ch = curl_init(); curl_setopt($ch, CURLOPT_URL,"$urladdress"); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HEADER, 0); //use this to suppress output 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);// tell cURL to graciously accept an SSL certificate 
$result = curl_exec ($ch) or die(curl_error($ch)); 
echo $result; 
echo curl_error($ch); 
curl_close ($ch);

感谢您的帮助

推荐答案

来自 手册:

成功时返回 TRUE,失败时返回 FALSE.但是,如果设置了 CURLOPT_RETURNTRANSFER 选项,它将在成功时返回结果,在失败时返回 FALSE.

Returns TRUE on success or FALSE on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure.

您的代码已经包含这一行(很好):

Your code already contains this line (which is good):

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

1 意味着您将从 $result = curl_exec ($ch) 收到一个解释性结果,而不仅仅是 true>假.

The 1 means you will receive an explanatory result back from $result = curl_exec ($ch) instead of just true or false.

因此,您的错误检查代码可能如下所示:

Your error checking code could therefore look like:

$result = curl_exec ($ch);
if($result === FALSE) {
    die(curl_error($ch));
}

您还可以检查通过 var_dump 返回的变量类型:var_dump($result).

You can also check they type of variable returned via var_dump: var_dump($result).

这篇关于PHP,卷曲.curl_exec 返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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