簡體   English   中英

通過 cURL 發布 json

[英]Post json via cURL

我有一個大問題。 我無法找到解決方案。 數據通過 GET 通過表單提交。 顯然,然后通過 AJAX 重新加載結果。我已經通過 cURL 嘗試了所有可能的方法。服務器甚至拋出 SQL 錯誤消息。 但不幸的是我無法讓它工作。 誰能幫我?

#{
#  "searchdata": {
#    "required": "1",
#    "statistics": "1",
#    "offer_name": null,
#    "zip": "01069",
#    "location": null,
#    "maxdistance": "5",
#    "target_group": null,
#    "age_group": null,
#    "language": null,
#    "sort": "zip_asc",
#    "record_offset": 0
#  }
#}

#Test-URL = https://pflegefinder.bkk-dachverband.de/aua/searchresult.php?searchdata%5Brequired%5D=1&searchdata%5Bstatistics%5D=1&searchdata%5Boffer_name%5D=&searchdata%5Bzip%5D=01069&searchdata%5Blocation%5D=&searchdata%5Bmaxdistance%5D=5&searchdata%5Btarget_group%5D=&searchdata%5Bage_group%5D=&searchdata%5Blanguage%5D=&searchdata%5Bsort%5D=zip_asc&searchdata%5Brecord_offset%5D=0

 $data = array("required" => "1","statistics"=>"1","offer_name" => null,"zip" => "51143","location" => null,"maxdistance" => "20","target_group" =>  null,"age_group" =>  null,"language" => null, "sort" => "zip_asc", "record_offset" => 0);
 $data_string = urlencode(json_encode($data));

    $url = "https://pflegefinder.bkk-dachverband.de/aua/rest_searchresult.php?".date("U");
    $ch = curl_init();
    // set post fields
    curl_setopt($ch, CURLOPT_URL, ($url));
#   curl_setopt($ch, CURLOPT_HTTPHEADER, array('Cookie: MK_CookieConsent={"required":true,"statistics":true},PHPSESSID="6ldq1ffekjeum41gr3b4bp5tqm"'));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    #curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    #curl_setopt($ch, CURLOPT_POSTFIELDS,'searchdata[required]=1&searchdata[statistics]=1&searchdata[offer_name]=&searchdata[zip]=01069&searchdata[location]=&searchdata[maxdistance]=5&searchdata[target_group]=&searchdata[age_group]=&searchdata[language]=&searchdata[sort]=distance_asc&searchdata[record_offset]=0');
    curl_setopt($ch, CURLOPT_POSTFIELDS,array("searchdata"=>$data_string));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Googlebot/2.1 (+http://www.google.com/bot.html)');
    curl_setopt($ch, CURLOPT_REFERER, 'https://pflegefinder.bkk-dachverband.de/aua/searchresult.php?searchdata%5Brequired%5D=1&searchdata%5Bstatistics%5D=1&searchdata%5Boffer_name%5D=&searchdata%5Bzip%5D=51143&searchdata%5Blocation%5D=&searchdata%5Bmaxdistance%5D=5&searchdata%5Btarget_group%5D=&searchdata%5Bage_group%5D=&searchdata%5Blanguage%5D=&searchdata%5Bsort%5D=distance_asc&searchdata%5Brecord_offset%5D=0');
#   curl_setopt($ch, CURLOPT_REFERER, 'https://pflegefinder.bkk-dachverband.de/aua/');
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_VERBOSE, '0');
    curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile2.txt");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile2.txt");
    curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());
    $seite = curl_exec($ch); 
    curl_close($ch);

laguage的拼寫錯誤(應該是language導致您看到的 SQL 錯誤。

我不再寫太多 PHP,我使用 Insomnia HTTP 客戶端輸入您的請求,這就是我發現錯誤的方式。 我導出了下面的代碼,這對我來說非常有用。

<?php

$curl = curl_init();

curl_setopt_array($curl, [
  CURLOPT_URL => "https://pflegefinder.bkk-dachverband.de/aua/rest_searchresult.php",
  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\t\"searchdata\": {\n\t\t\"required\": 1,\n\t\t\"offer_name\": null,\n\t\t\"zip\": 51143,\n\t\t\"location\": null,\n\t\t\"maxdistance\": 20,\n\t\t\"target_group\": null,\n\t\t\"age_group\": null,\n\t\t\"language\": null,\n\t\t\"sort\": \"zip_asc\",\n\t\t\"record_offset\": 0\n\t}\n}",
  CURLOPT_COOKIE => "PHPSESSID=2a4tctjc0ec6potr1p5a0tonir",
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM