生成POST请求并通过API发送json数据

时间:2016-11-11 18:49:02

标签: php json api

我正在尝试将我的PHP脚本与API集成到Market Hero电子邮件系统。

他们的API说明说:

  

为此网址生成POST请求:   //api.markethero.io/v1/api/tag-lead并发送json数据。

数据应如下所示:

{
 "apiKey":"38883ca614e36881995a16a54a060d4309870143acd40203832d3260b65deaa9",
 "firstName":"John",
 "lastName":"Doe",
 "email":"john.doe@mail.com",
 "tags":["Tag1", "Tag2"]
}

我曾尝试搜索和使用在此处和通过Google找到的API示例,但尚无效。

我已经测试了这个(得到here的代码):

<?php

//API Url
$url = '//api.markethero.io/v1/api/tag-lead';

//Initiate cURL.
$ch = curl_init($url);

//The JSON data.
$jsonData = array(
'apiKey' => 'myapikeyhere',
'firstName' => 'Test',
'lastName' => 'Tester',
'email' => 'some@mailinator.com',
'tags' => 'Tag2'
);

//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);
echo '1';
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);
echo '2';
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
echo '3';
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 
echo '4';
//Execute the request
$result = curl_exec($ch);
echo 'Done';
?>

回声1,2,3,4等只是为了瞥一眼执行停止的地方。但它一路走来&#34;完成&#34;。

我做错了什么?

市场英雄是新的,他们的开发人员现在有太多的帮助我这个,我理解这一点。我希望你们中的一些人能够帮助我。

PS:我也尝试过将此作为添加多个标签的JSON数据。

//The JSON data.
    $jsonData = array(
    'apiKey' => 'myapikeyhere',
    'firstName' => 'Test',
    'lastName' => 'Tester',
    'email' => 'some@mailinator.com',
     tags =>array("tag1", "tag2")
    );

0 个答案:

没有答案
相关问题