使用我的代码,我从一个网站接收数据。我想通过REST-API将此数据传输到数据库字段中。这应该与cURL(PUT)一起使用。因此,我需要在脚本中运行几个cURL来填充不同的数据库字段。为此,我使用了来自php.net(http://php.net/manual/de/function.curl-multi-init.php)的示例代码。不幸的是,我仅收到“ 400错误的请求-您的浏览器发送了无效的请求”。我在做什么错了?
我收到数据的密码
header('Content-type: text/html; charset=utf-8');
$User_Agent = 'Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0';
$url = "http://hub.culturegraph.org/entityfacts/118540238";
$url2 = "https://bpk.bs.picturemaxx.com/api/v1/editing/classifications/42/elements/2156190";
$url3 = "https://bpk.bs.picturemaxx.com/api/v1/editing/classifications/42/elements/2156191";
$request_headers[] = 'Accept: application/json';
$request_headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$request_headers[] = 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3';
$request_headers[] = 'Content-Type: application/json; charset=utf-8';
$request_headers[] = 'Accept-Encoding: gzip, deflate, identity';
$request_headers[] = 'Accept-Language: de,en-US;q=0.7,en;q=0.3';
$request_headers[] = 'X-picturemaxx-api-key: key';
$request_headers[] = "Authorization: Bearer token";
$request_headers[] = 'Expect: ';
// build the individual requests, but do not execute them
$ch = curl_init($url);
// Initiate curl
curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, "");
$result = curl_exec($ch); // Performs the Request, with specified curl_setopt() options (if any).
curl_close($ch);
// Closing
$data = json_decode($result, true);
使用以下代码确定要传输的数据,将其插入到要传输的表单中并使用json_encode。
$alternativData = array();
foreach($data['variantName'] as $alternativ) {
//echo $alternativ . " ; ";
$alternativData[] = $alternativ;
}
$dataj = array (
'classification_element_parent_id' => 0,
'classification_element_matchcode' => '',
'classification_element_foreignref' => '',
'localized' =>
array (
'en-us' =>
array (
'classification_element_name' => '',
),
'de-de' =>
array (
'classification_element_name' => substr(implode(' ; ', $alternativData),0 ,10000),
),
),
);
$data_json = json_encode($dataj);
$request_headers[] = 'Content-Length: ' . strlen($data_json);
$dataj2 = array (
'classification_element_parent_id' => 2156190,
'classification_element_matchcode' => '',
'localized' =>
array (
'en-us' =>
array (
'classification_element_name' => '',
),
'de-de' =>
array (
'classification_element_name' => $data['dateOfBirth'],
),
),
);
$data_json2 = json_encode($dataj2);
$request_headers[] = 'Content-Length: ' . strlen($data_json2);
然后跟随curl_multi_init,它导致400 Bad请求。
$ch_2 = curl_init($url2);
$ch_3 = curl_init($url3);
curl_setopt( $ch_2, CURLOPT_URL, $url2 );
curl_setopt($ch_2, CURLOPT_USERAGENT, $User_Agent);
curl_setopt($ch_2, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch_2, CURLOPT_ENCODING, "");
curl_setopt($ch_2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_2, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch_2, CURLOPT_POSTFIELDS, $data_json);
curl_setopt( $ch_3, CURLOPT_URL, $url3 );
curl_setopt($ch_3, CURLOPT_USERAGENT, $User_Agent);
curl_setopt($ch_3, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch_3, CURLOPT_ENCODING, "");
curl_setopt($ch_3, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_3, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch_3, CURLOPT_POSTFIELDS, $data_json2);
// build the multi-curl handle, adding both $ch
$mh = curl_multi_init();
curl_multi_add_handle($mh, $ch_2);
curl_multi_add_handle($mh, $ch_3);
$active = null;
// Handles ausführen
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc == CURLM_OK) {
if (curl_multi_select($mh) == -1) {
usleep(100);
}
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
//close the handles
curl_multi_remove_handle($mh, $ch_2);
curl_multi_remove_handle($mh, $ch_3);
curl_multi_close($mh);
$response_1 = curl_multi_getcontent($ch_2);
$response_2 = curl_multi_getcontent($ch_3);
echo "$response_1 $response_2"; // output results
var_dump(curl_getinfo($ch));
var_dump(curl_getinfo($ch_2));
var_dump(curl_getinfo($ch_3));
在此先感谢您的帮助和建议。