drfls28608 2014-09-12 08:35
浏览 145

将编码的URL转换为JSON对象

I've never worked with JSON objects before but I think this is probably the best route for what I'm trying to achieve (if you think there is a better option please let me know, though I cannot use SOAP)

I am trying to submit some data via POST to an API in order to add some data to a newsletter list which is stored as XML. Here is the documentation for the request I am trying to complete: https://api.dotmailer.com/v2/help/wadl#PostAddressBookContacts

When visiting the API url and authenticating I see data laid out in the following:

<ArrayOfApiContact xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://apiconnector.com">
  <ApiContact>
    <Id>1058905201</Id>
    <Email>email@email.co.uk</Email>
    <OptInType>Unknown</OptInType>
    <EmailType>Html</EmailType>
    <DataFields i:nil="true"/>
    <Status>Subscribed</Status>
  </ApiContact>
</ArrayOfApiContact>

So I have attempted to replicate as such, using a variable instead of a hardcoded email:

$xml = array(
    'id' => urlencode('123456789'),
    'email' => urlencode($useremail),
    'optInType' => urlencode('Unknown'),
    'emailType' => urlencode('Html'),
    'dataFields' => urlencode(':null'),
    'status' => urlencode('Subscribed')
);

I am combining the array here:

foreach($xml as $key=>$value) { $xml_string .= $key.'='.$value.'&'; }

And then using cURL to POST the request to the API, with my authentication (apipassword and apiemail which the username)

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$auth_url);
curl_setopt($ch,CURLOPT_POST, count($xml));
curl_setopt($ch,CURLOPT_POSTFIELDS, $xml_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$apiemail:$apipassword");
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code
$result = curl_exec ($ch);
curl_close ($ch);

I have already tested this without the POST and I do indeed get the data back in this form:

[{"id":1058905201,"email":"email@email.co.uk","optInType":"Unknown","emailType":"Html","dataFields":null,"status":"Subscribed"}]

So I know the auth is working and the data is coming back correctly, but now when I try to POST my data I get:

{"message":"Content type \"application/x-www-form-urlencoded\" is not supported. Please use one of: application/json,application/xml ERROR_CONTENT_TYPE_IS_NOT_SUPPORTED"}

So how do I convert my array which is URL encoded into something readable by the API?

  • 写回答

2条回答 默认 最新

  • dongmangji0950 2014-09-12 08:52
    关注

    Try a content type of application/json like so:

    curl_setopt($ch, CURLOPT_HTTPHEADER, array('content-type: application/json'));
    
    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致