-1

the jquery function below is a sample script from boomerang. It is their GetSendList function. I was trying to make a php version of it but still unsuccessful. Here's the function:

function GetSendList() {
    var r = new Object;

    r = [
        {
            email: $("#txt-SendList-email-0").val(),
            jobNumber: $("#txt-SendList-jobNumber-0").val()
        },
        {
            email: $("#txt-SendList-email-1").val(),
            jobNumber: $("#txt-SendList-jobNumber-1").val()
        }
    ]


    var dataString = JSON.stringify(r);

    $.ajax({
        type: "POST",
        url: "https://target.boomerang.com/V1.0/api/SendList",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        data: dataString,
        headers: { auth_token: $('#txtAuthenticationCode').val() },
        success: function (response) {
        }
    });
}

Here's my PHP script:

$main_url = "https://target.boomerang.com/V1.0/api/";
$token = "xxx";

$values = array('email'=>'email',
    'jobNumber'=>'jobnumber'
    );

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$main_url."SendList");
curl_setopt($ch,CURLOPT_HTTPHEADER,array('auth_token: '.$token));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($values));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec ($ch);
curl_close ($ch);
$output = json_decode($server_output);

I'm guessing I have a wrong formatting for my values that's why I'm unsuccessful. I do not know the equivalence of jquery object in PHP.

PS. I use the same php script to call their other functions and Im successful with those. It's only the GetSendList that I'm having problem right now. Please advice. Thanks!

user3360031
  • 627
  • 4
  • 13
  • 21
  • FYI: *Don't* use `async: false`. It will cause the browser to lock up. – gen_Eric Apr 14 '14 at 18:50
  • I think your PHP version is missing the SSL options and cacert.pem file, otherwise you can't do HTTPS. – adeneo Apr 14 '14 at 18:51
  • See http://stackoverflow.com/questions/931407/what-is-stdclass-in-php – elclanrs Apr 14 '14 at 18:51
  • 2
    jQuery object? HUH? Do you mean an [object literal](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literals#Object_literals)? – epascarello Apr 14 '14 at 18:52
  • It's actually not my script. It is the boomerang's demo script. And I'm converting it to PHP. – user3360031 Apr 14 '14 at 18:52
  • 1
    `r = new Object; r = ...`? What was the point of `new Object`? And since when does anybody write `new Object` over the literal `{}` – Paul S. Apr 14 '14 at 18:52
  • So, what does `$server_output` contain? What does "unsuccessful" mean? Do you see any errors? Incorrect data? – gen_Eric Apr 14 '14 at 18:52
  • P.S. `[]` is an array, not an object. And you can just do `var r = [...]`. You are creating a new object, then throwing it away and using an array instead. – gen_Eric Apr 14 '14 at 18:57
  • 1
    The API is expecting JSON, but you're sending over a plain form-encoded data string. – Marc B Apr 14 '14 at 19:00
  • Ok so let me clear things up. First, I meant Object variables, not class. Second, I dont think it's an issue on SSL since other functions apart from GetSendList is working. Third, the $server_output only displays how many emails are successfully queued. But I kept getting 0 so basically it didn't work. Though it did not generate any error or incorrect data. – user3360031 Apr 14 '14 at 19:01
  • Add curl_error() and see what the problem is, I'll bet 10 cents is missing authentication for SSL as you shouldn't be able to do https with cURL without a .pem file. – adeneo Apr 14 '14 at 19:46
  • @adeneo tried it. generated no error. – user3360031 Apr 14 '14 at 19:59

1 Answers1

2

to make an object on PHP you just need to do :

$object = (object) array(); // Or new stdClass();

Use :

$object->name = "John";
echo $object->name;

Use with Javascript :

$object = json_decode( $_POST['json'] );
$json = json_encode( $object ); 
Arthur
  • 4,870
  • 3
  • 32
  • 57