2

这段代码有什么问题?

不知道为什么,但格式在这里搞砸了。这是行:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

我是新来的,所有的帮助都得到了赞赏。谢谢。

<?php
if(isset($_POST['payment'])){
    require('config.php');
    $ch= curl_init('https://coinbase.com/api/v1/account/generate_receive_address');
    $amount=$_POST['amount'];
    $json=json_encode(array("api_key"=>COINBASE_APIKEY,"address"=>array("callback_url"=>$_SERVER['HTTP_HOST'] . dirname($_SERVER['REQUEST_URI']).'callback.php')));
    curl_setopt($ch, CURLOPT_HTTPHEADERS, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
    curl_setopt($ch, CURLOPT_POST, 1);
    $jsonreturn=json_decode(curl_exec($ch),true);
    if(is_integer($amount) && $amount>0 && $jsonreturn['callback_url']==$_SERVER['HTTP_HOST'] . dirname($_SERVER['REQUEST_URI']).'callback.php'){
        $mysql=new mysqli(MYSQL_HOSTNAME,MYSQL_USERNAME,MYSQL_PASSWORD,MYSQL_DATABASE);
        $mysql->query("INSERT INTO invoices(Amount, Address) VALUES ('$amount','".$jsonreturn['address']."')");
        $mysql->close();
        header('refresh: 1;url=payment.php?address='.$jsonreturn['address'].'&amount='.$amount);
        exit();
    }else{
        header("refresh: url=payment.php?error=1");
        exit();
    }
}
?>
4

1 回答 1

3

The problem is actually with the CURLOPT_HTTPHEADERS line. (line numbers start from the beginning of the file, not just counting lines of PHP code)

The actual PHP constant is CURLOPT_HTTPHEADER (singular), not CURLOPT_HTTPHEADERS.

Because of the way PHP handles undefined constants, it generates a warning message (probably hidden due to your config settings), and treats the undefined constant as a string: "CURLOPT_HTTPHEADERS" - thus the error message about expecting an integer (long), and getting a string.

Updated

If you're still not getting the results you expect from your code, I'd check the following:

  1. Your connection parameters - where do the hostname, username, password, etc come from? are you sure they're correct.

  2. You haven't properly escaped your variable values before inserting them into your query. If you've got quotes in your values, it'll cause problems. Try echoing out your sql instead of querying it, and then try to run it manually against your database. I'd also highly recommend looking into using prepared statements instead.

于 2013-06-22T06:14:49.107 回答