我正在努力实现以下目标:
第1步和第2步工作正常,但是当我尝试在我的代码中使用 wp_insert_post()函数时,它们全部停止工作,发送服务器收到http 500响应。
我有一个名为import.php的文件,该文件接收来自发送服务器的呼叫。该填充仅调用getCars函数。
我已经用更简单的SQL语句测试了XML,删除了$ conn以进行发布,但是它可以工作。
为什么我的wp_insert_post()不起作用?我读过有关包括wp-load和东西的信息,但是那也不起作用。请注意,这段代码都在WordPress插件中。
<?php
function getCars(){
/*if($_SERVER['REMOTE_ADDR'] != '90.145.213.154') {
// Some security
exit;
}*/
$xmldoc = file_get_contents('php://input');
$xml = parseXml($xmldoc);
// Create connection
switch((string)$xml['actie']) {
case 'add':
// Controles uitvoeren
controleer_voertuig($xml);
// Add vehicle to DB
/*$sql = 'INSERT INTO voertuigen (voertuignr_hexon, kenteken, merk) VALUES ('. (string) $xml->voertuignr_hexon .', "'. addslashes((string) $xml->kenteken) .'", "'. addslashes((string) $xml->merk) .'")';*/
// Create post object
$args = array(
'post_title' => (string) $xml->voertuignr_hexon,
'post_content' => 'content',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8,39 )
);
// Insert the post into the database
wp_insert_post($args, $wp_error=true);
print_r($wp_error);
// ...
// Foto's opslaan
//verwerk_fotos($xml->afbeeldingen);
break;
case 'change':
// Controles uitvoeren
controleer_voertuig($xml);
// Voertuig wijzigen in database
/*$sql = 'UPDATE voertuigen SET kenteken = "'. addslashes((string) $xml->kenteken) .'", merk = "'. addslashes((string) $xml->merk) .'" WHERE voertuignr_hexon = '. $xml->voertuignr_hexon .' LIMIT 1';*/
// ...
// Foto's opnieuw ophalen
//verwerk_fotos($xml->afbeeldingen);
break;
case 'delete':
// Voertuig verwijderen uit database
/*$sql = 'DELETE FROM voertuigen WHERE voertuignr_hexon = '. (string) $xml->voertuignr_hexon .' LIMIT 1'; */
// ...
break;
}
print("1");
}
function parseXml($xmldoc) {
// XML inlezen met je favoriete XML-parser. Het voorbeeld gebruikt simpleXML.
$xml = @simplexml_load_string($xmldoc);
if(!$xml) {
// Foutmelding teruggeven aan server van Hexon
print("Geen geldige XML ontvangen");
//exit;
}
return $xml;
}
function controleer_voertuig($xml) {
/*if(empty($xml->fotos)) {
// Foutmelding teruggeven aan server van Hexon
print("Op de eigen website zijn alleen voertuigen met foto toegestaan");
exit;
}*/
}
function verwerk_fotos($fotos) {
foreach($fotos as $foto_nr => $foto_url) {
$bestandsnaam = 'fotos/'. $_GET['hexon_nr'] .'-'. $foto_nr .'.jpg';
$imgdata = file_get_contents($foto_url);
file_put_contents($bestandsnaam, $imgdata);
}
}
编辑 我使用了DEBUG_LOG,但它只显示一些旧错误,并且与不知道特定功能无关。另外,当我删除WordPress函数wp_insert_post()时,该函数运行良好。最后输出“ 1”。
wp-config.php
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', true);
日志(将$ wp-error添加到wp_insert_post之后)
[20-Sep-2018 08:42:59 UTC] PHP Fatal error: require_once(): Failed opening required '/home/vehikel/domains/vehikel.com/public_html/dev//includes/post.php' (include_path='.:/usr/local/php71/lib/php') in /home/vehikel/domains/vehikel.com/public_html/dev/wp-content/plugins/import-wheeler-delta/index.php on line 18
[20-Sep-2018 09:53:34 UTC] PHP Notice: Undefined offset: 0 in /home/vehikel/domains/vehikel.com/public_html/dev/wp-includes/class-wp-query.php on line 3071