PHP,卷曲。curl_exec 返回什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16452636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:09:54  来源:igfitidea点击:

PHP, CURL. What does curl_exec return?

phpcurl

提问by shadyhossin

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.

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

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);

Thank you for your help

感谢您的帮助

回答by hek2mgl

From the manual:

手册

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.

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

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

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

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

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

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

Your error checking code could therefore look like:

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

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

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

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