如何在函数内传递变量?

时间:2017-08-16 10:27:06

标签: php

我正在使用API 并将一个变量传递给API的函数,但它在这种情况下工作: curl_init(' https://www.coinpayments.net/api.php?txid=123456&#39);

我想通过变量传递 txid 值,我尽我所能,但上面的功能不能通过以下方式工作:

$value="https://www.coinpayments.net/api.php?txid=".$txnid;

        curl_init($value); 

    curl_init('https://www.coinpayments.net/api.php?txid=$txnid'); 
    curl_init('https://www.coinpayments.net/api.php?txid='$txnid''); 
    curl_init("https://www.coinpayments.net/api.php?txid='".$txnid."'"); 
    curl_init("https://www.coinpayments.net/api.php?txid=$txnid"); 

1 个答案:

答案 0 :(得分:0)

当我使用浏览器触发api url时    它显示错误

API requests can only be made via POST. For documentation see: https://www.coinpayments.net/apidoc

所以你必须使用下面的代码

将变量作为post参数传递
<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://www.coinpayments.net/api.php",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{'txn_id':".$txn_id."}",
  CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache",
    "content-type: application/json"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
?>

当我使用上面的代码时,我从上面的网址得到了回复

{
   "error": "No HMAC signature sent",
   "result": []
}