-1

我必须通过 curl 以 json 格式发送我的数据来创建一个新的商家应用程序。我收到了来自我的支持团队的邮件。

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;

但我得到“HTTP 状态 405 - 不支持请求方法 'GET'”。

请告诉我我在 curl 中的错误,因为正如邮件中提到的,我必须使用 X GET 并且我只使用 GET。

4

2 回答 2

0

我在评论一些标题后得到了成功响应,请查看评论代码,

$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); 
于 2016-01-19T05:02:16.913 回答
0

/merchant/create 不太可能接受 GET 方法,因为它在请求中说 PUT,您应该尝试使用 PUT,即使更明显的选择是尝试使用 POST(并使用 PUT 进行更新)。

于 2016-01-04T10:59:17.870 回答