我有问题。 我写了一个api连接,将数据插入数据库。
即使我在“邮递员”上尝试使用此工具也能正常工作。
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://dev.daypaio.com/api/transaction?access_token=here are the accesstoken",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\n \"transactionID\": \"98892374982739847\",\n \"note\": \"hier ist eine note\",\n \"referer\": \"http://www.google.de\",\n \"createOrder\": true,\n \"coupon\": \"coupon2018\",\n \"newsletter\": true,\n \"billingAdress\": {\n \"email\": \"is@daniel-mauer.de\",\n \"phone\": \"3243245345\",\n \"cell\": \"435654645645\",\n \"ext_id\": 0,\n \"first_name\": \"Inge\",\n \"last_name\": \"Sockenschuss\",\n \"street\": \"Sockenstreet 123\",\n \"zip\": \"12345\",\n \"city\": \"Sockenhausen\",\n \"country\": \"Germany\",\n \"dob\": \"1980-12-12\",\n \"demographic\": {\n \"gender\": \"female\",\n \"job\": \"Socken CEO\",\n \"nationality\": \"german\",\n \"disability\": \"none\"\n }\n },\n \"attributes\": [\n {\n \"name\": \"\",\n \"value\": \"\"\n }\n \n ],\n \"aattributes\": [\n {\n \"name\": \"\",\n \"value\": \"\"\n }\n ],\n \"connections\": [\n {\n \"kind\": \"\",\n \"t\": \"contact\",\n \"tags\": [\n \"...\",\"...\"\n ],\n \"email\": \"\",\n \"phone\": \"\",\n \"cell\": \"\",\n \"first_name\": \"\",\n \"last_name\": \"\"\n }\n ],\n \"products\": [\n {\n \"pos\": 1,\n \"name\": \"\",\n \"tax\": 0,\n \"price\": 0,\n \"desc\": \"\",\n \"category\": \"\",\n \"unit\": \"\",\n \"qty\": 1,\n \"typ\": \"\",\n \"brand\": \"\",\n \"start\": \"\",\n \"end\": \"\",\n \"duration\": 1,\n \"durationUnit\": \"days\",\n \"sku\": \"\"\n }\n ],\n \"total\": 0,\n \"sum\": 0\n}",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/json",
"postman-token: def0a6ec-96b4-7983-a32b-2431fddf7812"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
这是邮递员的有效代码。 但是当我尝试动态地将其创建到php中时,curl请求将变为超时,请在数据库中插入条目。
此处是php代码:
$json_array = array(
'transactionID' => time().mt_rand(),
'note' => 'hier kommen notes hinein',
'referer' => $_SERVER['HTTP_REFERER'],
'createOrder' => true,
'coupon' => generateCouponCode(),
'newsletter' => true,
'billingAddress' => array(
'email' => 'email@daniel-mauer.de',
'phone' => '1234567890',
'cell' => '123456756867',
'ext_id' => mt_rand(),
'first_name' => 'Daniel',
'last_name' => 'Mauer',
'street' => 'Am Tierpark 8',
'zip' => '98527',
'city' => 'Suhl',
'country' => 'Deutschland',
'dob' => '1985-11-01',
'demographic' => array(
'gender' => 'male',
'job' => 'Developer',
'nationality' => 'deutsch',
'disability' => 'none'
),
'danielField' => 'daniel sein feld'
),
);
$json_array_short = array(
'transactionID' => time().mt_rand(),
'note' => 'hier kommen notes hinein',
'referer' => $_SERVER['HTTP_REFERER'],
'createOrder' => true,
'coupon' => generateCouponCode()
);
$jsonObject = json_encode($json_array);
$jsonObjectShort = json_encode($json_array_short);
$curl = curl_init();
//print_r($curl);
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => "https://dev.daypaio.com/api/transaction?access_token=token comes here",
CURLOPT_POST => 1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $jsonObjectShort,
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json"
),
));
//print_r('<br /><br />' . $curl);
$response = curl_exec($curl);
print_r($response . '<br /><br />');
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
if(!curl_exec($curl)){
die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}