在GET方法中使用curl发送数据时出错

时间:2016-01-04 10:42:51

标签: php json curl get xgettext

我必须通过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'不受支持”。

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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);