-1

I have to send my data in json format via a curl to create a new merchant application.I got mail from my support team.

curl -D- -X GET -H "Authorization: Basic Y2NhcGlAY2FyZGNvR05hOQ==" -H 'Accept: application/json' -H 'Content-type: application/json' -X PUT 'https://testapi.xyz.com/agentcenter/merchant/create' -d '{"merchant":{"firstName":"mike","lastName":"test","email":"mm@mandm.net","address":"123 test ln","city":"rock hill","state":"PA","zip":"19406","ownerDob":"19871218","ownerSsn":"123-12-1234","phone":"555-555-5555","url":"http:\/\/www.testurl.com","physicalAddress":"1234 ebenezer rd","physicalCity":"rock hill","physicalState":"PA","physicalZip":"19406","application":{"bankRouting":"036001808","bankAccount":"123456789","amexMid":null}}}'.

My line of code is:-

$ch = curl_init('https://testapi.com/merchant/create');

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");

curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string))
);

curl_setopt($ch, CURLOPT_TIMEOUT, 5);

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);

$result = curl_exec($ch);

curl_close($ch);

echo $result;

But I got "HTTP Status 405 - Request method 'GET' not supported".

Please tell me my mistake in curl because as mention in mail i have to use X GET and I am using only GET.

Meenesh Jain
  • 2,532
  • 2
  • 19
  • 29
Ritesh
  • 81
  • 12

2 Answers2

0

It's unlikely for /merchant/create to be accepting GET method and since it says PUT in the request, you should try with PUT even though the more obvious option would be to try with POST (and use PUT for updating).

Rok D.
  • 244
  • 1
  • 9
  • I tried all the method and then also I Got the error. – Ritesh Jan 04 '16 at 11:06
  • `curl_setopt($ch, CURLOPT_POST, 1);` for post or `curl_setopt($ch, CURLOPT_PUT, 1);` for put – Rok D. Jan 04 '16 at 11:22
  • But I am using Get method. One more thing I have just tested, is that when I executed the curl over command line it work fine but when I run by using curl it show error. – Ritesh Jan 04 '16 at 11:28
  • You can see from the response that GET is not supported and you can see in the curl command that there is also -X PUT before the json – Rok D. Jan 04 '16 at 11:32
  • Now I Update my code like this.. $ch = curl_init($theurl); ... curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); // -X .... ... curl_setopt($ch, CURLOPT_PUT, 1); ... echo $response = curl_exec($ch); die; But now i am getting blank response. – Ritesh Jan 04 '16 at 12:21
0

I got success response after commenting some header,Please see the commented code,

$jsondata = json_encode($postData);

$url =  'url';

  $headers= array('Authorization: Basic ' . base64_encode($username . ":" .              
  $password),'Accept: application/json','Content-Type: application/json');

curl_setopt($curl, CURLOPT_URL, $url);

//curl_setopt($curl, CURLOPT_PUT, true);

curl_setopt($curl, CURLOPT_HEADER, 1);

curl_setopt($curl, CURLOPT_HTTPHEADER,$headers);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');

curl_setopt($curl, CURLOPT_POSTFIELDS,$jsondata);

//curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);

//curl_setopt($curl, CURLOPT_USERPWD, base64_encode("$username:$password"));

$result = curl_exec($curl);

print_r($result);

curl_close($curl); 
Meenesh Jain
  • 2,532
  • 2
  • 19
  • 29
Ritesh
  • 81
  • 12