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'));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题