2

What's wrong with this code?

Idk why but the formatting is messed up on here. It's the line:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

I'm new here and all help is appreicated. Thanks.

<?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();
    }
}
?>
Wiseguy
  • 20,522
  • 8
  • 65
  • 81
Jake McLaughlin
  • 83
  • 1
  • 2
  • 7

1 Answers1

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.

Community
  • 1
  • 1
jcsanyi
  • 8,133
  • 2
  • 29
  • 52