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

查看:171
本文介绍了PHP,CURL. 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)收到返回的解释性结果,而不仅仅是truefalse.

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. curl_exec返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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