Hallo,
ich habe eine Frage, und zwar habe ich ein cURL Request erstellt um etwas aus shopware an eine externe api zu senden.
Wenn ich folgendes Format verwende funkiert alles einwandfrei ohne timeout und co.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "url zu meiner api",
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\" : \"8187623438764\", \n \"referer\" : \"https://shopware.daniel-mauer.de/\",\n \"createOrder\" : true, \n \"billingAdress\" : {\n \"email\" : \"is@daniel-mauer.de\", \n \"phone\" : \"01234567890\", \n \"cell\" : \"\", \n \"ext_id\" : \"1\", \n \"salutation\" : \"Herr\", \n \"company\" : \"daypaio GmbH\", \n \"first_name\" : \"Inge\", \n \"last_name\" : \"Sockenschuss\", \n \"street\" : \"\", \n \"zip\" : \"\", \n \"city\" : \"\", \n \"country\" : \"\", \n \"postbumber\" : \"\", \n \"packstationnumber\" : \"\"\n }, \n \"shippingAdress\" : {\n \"company\" : \"\", \n \"first_name\" : \"\", \n \"last_name\" : \"\", \n \"street\" : \"\", \n \"zip\" : \"\", \n \"city\" : \"\", \n \"postbumber\" : \"\", \n \"packstationnumber\" : \"\"\n }, \n \"attributes\":[\n {\n \"name\":\"herkunft\",\n \"value\":\"Suchmaschine\"\n }\n],\n \"products\" : [\n {\n \"pos\":1,\n \"sku\" : \"NZW020\", \n \"name\": \"All-In-Paket - Neuseeland mit China Airline\",\n \"external\" : false, \n \"tax\" : 19, \n \"discount\" : 0, \n \"price\" : 1420.16, \n \"unit\" : \"Pauschal\", \n \"qty\" : 1, \n \"desc\" : \"All-In-Paket - Neuseeland mit China Airline\", \n \"typ\" : \"Normal\",\n \"brand\": \"Praktikawelten\",\n \"category\": \"Work and Travel\",\n \"start\": \"2017-12-01\",\n \"end\": \"2017-12-07\",\n \"duration\": 7,\n \"durationUnit\": \"days\"\n },\n {\n \"pos\":2,\n \"sku\" : \"4711\", \n \"name\": \"Versicherungspaket XL mit Reiserücktritt\",\n \"external\" : false, \n \"tax\" : 19, \n \"discount\" : 0, \n \"price\" : 0, \n \"unit\" : \"Pauschal\", \n \"qty\" : 1, \n \"desc\" : \"Versicherungspaket XL mit Reiserücktritt\", \n \"typ\" : \"Normal\",\n \"brand\": \"Praktikawelten\",\n \"category\": \"Work and Travel\",\n \"start\": \"2017-12-01\",\n \"end\": \"2017-12-07\",\n \"duration\": 7,\n \"durationUnit\": \"days\"\n },\n {\n \"pos\":3,\n \"sku\" : \"12345\", \n \"name\": \"Zwischenstop Dubai\",\n \"external\" : false, \n \"tax\" : 19, \n \"discount\" : 0, \n \"price\" : 200, \n \"unit\" : \"Pauschal\", \n \"qty\" : 1, \n \"desc\" : \"Zwischenstop Dubai\", \n \"typ\" : \"Normal\",\n \"brand\": \"Praktikawelten\",\n \"category\": \"Work and Travel\",\n \"start\": \"2017-12-02\",\n \"end\": \"2017-12-03\",\n \"duration\": 1,\n \"durationUnit\": \"days\"\n }\n],\n \"connections\":[\n {\n \"kind\": \"Notfallkontakt\",\n \"t\":\"contact\",\n \"tags\": [\"Notfallkontakt\"],\n \"email\" : \"mh@curesoft.de\", \n \"phone\" : \"080226634680\", \n \"cell\" : \"\", \n \"ext_id\" : \"2\", \n \"salutation\" : \"Herr\", \n \"company\" : \"\", \n \"first_name\" : \"Maximilian\", \n \"last_name\" : \"Hanrieder\", \n \"street\" : \"\", \n \"zip\" : \"\", \n \"city\" : \"\", \n \"country\" : \"\"\n \n }\n ],\n \"net_total\" : 1620.16, \n \"total\" : 1890, \n \"shippingCosts\" : 0,\n \"sum\" : 1890\n}",
//CURLOPT_POSTFIELDS => json_encode($data_array);
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Verwende ich aber folgendes
function daypaioApiCall($method, $url, $data)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "url zu meiner api",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"Content-Type: application/json"
),
));
switch ($method){
case "POST":
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
// EXECUTE
$response = curl_exec($curl);
$err = curl_error($curl);
// CLOSE CONNECTION
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
print_r($url);
}
curl_close($curl);
$close_connection = curl_close($curl);
echo $close_connection;
}
$data_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' => 'INge',
'last_name' => 'Sockenschuss',
'street' => 'Sockenstraße 1234',
'zip' => '12345',
'city' => 'Sockenhausen',
'country' => 'Deutschland',
'dob' => '1985-11-01',
'demographic' => array(
'gender' => 'male',
'job' => 'Developer',
'nationality' => 'deutsch',
'disability' => 'none'
),
'danielField' => 'daniel sein feld'
),
);
function generateCouponCode($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
$apiUrl = "https://app.daypaio.com/api/transaction?access_token=111111111221212121212112121212121212121212121212121212";
$req = daypaioApiCall("POST", $apiUrl, $data_array);
print_r($req);
Bei dieser Version timed der cURL Request immer aus, und die Daten befinden sich dann trotzdem in der externen DB.
Ich hoffe ihr könnt mir weiterhelfen, Vielen Dank.