user1092390
user1092390

Reputation: 13

Weclapp PHP API { "error": "invalid data" }

I am trying to use Weclapp API by using Curl and PHP. So far i have the following code

$ch = curl_init("https://xxx.weclapp.com/webapp/api/v1/contact");

$data = array( 
  "'lastName' : 'Bar'", 
  "'firstName' : 'Foo'"); 
$data_string = json_encode($data); 

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(   
    "AuthenticationToken: xxxxxx",                                                                       
    'Content-Type: application/json')                                                                       
);                                                                                                                   

$result = curl_exec($ch);
curl_close ($ch);

Unfortunately the result is { "error": "invalid data" }

If i use the following bash cmd it works:

curl -H "AuthenticationToken:xxxx" -H "Content-type: application/json" -X POST -d '{"lastName" : "Bar", "firstName" : "Foo"}' https://xxxx.weclapp.com/webapp/api/v1/contact

So where is the difference between my PHP Code and that bash CMD?

Thank you!

Upvotes: 1

Views: 618

Answers (1)

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26258

The error is here:

$data = array( 
  "'lastName' : 'Bar'", 
  "'firstName' : 'Foo'"); 

This is not a valid php array format. Change it to:

$data = array( 
'lastName'  => 'Bar', 
'firstName' => 'Foo'
);

Upvotes: 2

Related Questions