我想自动从其他网站获取产品数据,或者通过抓取它,或者通过使用cURL访问API。由于我们的网站使用Wordpress,我正在尝试制作一个插件。我现在尝试在插件的设置页面上获取字段,以填写网站名称、cURL的链接格式以及应该导入的产品ID。插件的设置页面上会有一个按钮,当再次单击时,该按钮会添加相同的字段。我试图使用一个对象类,因为我想使用多个网站。我在我们的网站上收到HTTP错误500,所以我认为在我的代码中“/**开始插件设置页面”部分之后我做错了什么。如果有人检查我的代码并给我一个正确的方向,那就太好了:)。我对PHP也很陌生,所以如果有关于如何使代码更具可读性或如何以更好的方式获取产品数据的提示,那就太好了!
你好,玛蒂恩
<?php
/**
* Plugin Name: Dropship Data Scraper
* Plugin URI: http://example.com/
* Description: This plugin scrapes data from websites and puts them in product pages
* Version: 1.0.0
* Author: Martijn
* Author URI: https://example.com/
* License: Proprietary
*/
//**START Adding custom fields to product options Inventory tab**
//Display Fields
add_action( 'woocommerce_product_options_inventory_product_data', 'woo_add_custom_general_fields' );
//Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
//Custom fields will be created here...
//Purchase Price Field
woocommerce_wp_text_input(
array(
'id' => '_purchase_price',
'label' => __( 'Purchase price', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Enter the purchase price here.', 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
//Product Link Field
woocommerce_wp_text_input(
array(
'id' => '_product_link',
'label' => __( 'Product link', 'woocommerce' ),
'placeholder' => 'http://',
'desc_tip' => 'true',
'description' => __( 'Enter the product link from the supplier here.', 'woocommerce' )
)
);
echo '</div>';
}
function woo_add_custom_general_fields_save( $post_id ){
//Purchase Price Field
$woocommerce_purchase_price = $_POST['_purchase_price'];
if( !empty( $woocommerce_purchase_price ) )
update_post_meta( $post_id, '_purchase_price', esc_attr( $woocommerce_purchase_price ) );
elseif( empty( $woocommerce_purchase_price ) )
update_post_meta( $post_id, '_purchase_price', NULL );
//Product Link Field
$woocommerce_product_link = $_POST['_product_link'];
if( !empty( $woocommerce_product_link ) )
update_post_meta( $post_id, '_product_link', esc_attr( $woocommerce_product_link ) );
elseif( empty( $woocommerce_product_link ) )
update_post_meta( $post_id, '_product_link', NULL );
}
//**END Adding custom fields to product options Inventory tab**
//**START Plugin Settings Page
//Settings page dropship ids class
class DropshipFields {
public static $counter = 0;
private $dropshipIds;
//Constructor to count how many DropshipFields objects are created
function __construct() {
self::$counter++;
}
//function to register settings
public function ff_dropship_data_scraper_settings() {
$this->dropshipIds = "dropship-ids" . BaseClass::$counter;
return register_setting( 'ff-dropship-data-scraper-settings-group', $this->dropshipIds );
}
//function to display the dropship fields in ff_dropship_data_scraper_settings_page()
public function displayFields() {
}
}
//Eventually I want to put here some code that makes a new object everytime a button is pushed on the settings page
$DropshipFields1 = new DropshipFields();
//Add menu item on admin side
add_action('admin_menu', 'ff_dropship_data_scraper_menu');
function ff_dropship_data_scraper_menu() {
add_menu_page('FF Dropship Data Scraper Settings', 'FF Dropship Data Scraper Settings', 'administrator', 'ff-dropship-data-scraper-settings', 'ff_dropship_data_scraper_settings_page', '
dashicons-clipboard');
}
add_action( 'admin_init', 'ff_dropship_data_scraper_settings' );
function ff_dropship_data_scraper_settings() {
$DropshipFields1->ff_dropship_data_scraper_settings();
}
function ff_dropship_data_scraper_settings_page() { ?>
<div class="wrap">
<h1>Dropship data</h1>
<form method="post" action="options.php">
<?php settings_fields( 'ff-dropship-data-scraper-settings-group' ); ?>
<?php do_settings_sections( 'ff-dropship-data-scraper-settings-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Dropship IDs</th>
<td><textarea placeholder="Your dropship product ids" name="dropship-ids" rows="5" cols="1000"><?php echo esc_attr( get_option('dropship-ids') ); ?></textarea></td>
</tr>
</table>
<table class="form-table">
<tr valign="top">
<th scope="row">Dropship IDs</th>
<td><textarea placeholder="Your dropship product ids" name="dropship-ids" rows="5" cols="1000"><?php echo esc_attr( get_option('dropship-ids') ); ?></textarea></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php }
//**END Plugin Settings Page
//**START cURL Scraper
foreach($colors as $value) {
//Variables
$url = "https://apibeta.banggood.com/getAccessToken?apiTest=1&apiTest=1app_id=&app_secret=";
$json;
//Initialize
$ch = curl_init();
//Set options
//Url to send the request to
curl_setopt($ch, CURLOPT_URL, $url);
//Return instead of outputting directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//Include header in the output, set to false
curl_setopt($ch, CURLOPT_HEADER, 0);
//Execute the request and fetch the response. Check for errors
$output = curl_exec($ch);
if ($output === FALSE) {
echo 'cURL ERROR: ' . curl_error($ch);
}
//Close and free up the cURL handle
curl_close($ch);
//decode json
$json = json_decode($output, true);
}
//**END cURL Scraper
//**START creating, updating or deleting dropship products
//Put dropship-ids in an array
$text = get_option('dropship-ids');
//explode all separate lines into an array
$textAr = explode("\n", $text);
//trim all lines contained in the array.
$textAr = array_filter($textAr, 'trim');
//Update dropship products from dropship-ids
//Array for the last dropship-ids
$last_ids = [];
//Update function
function update_product_data() {
foreach($textAr as $value) {
if (in_array($value, $last_ids)) {
}
foreach($last_ids as $value) {
if (in_array($value, $textAr)) {
}
}
}
}
//**END creating, updating or deleting dropship products
?>
试试这个代码
我已经修改了这一行$Dropship Fields1-
和
add_menu_page
代码
<?php
/**
* Plugin Name: Dropship Data Scraper
* Plugin URI: http://example.com/
* Description: This plugin scrapes data from websites and puts them in product pages
* Version: 1.0.0
* Author: Martijn
* Author URI: https://example.com/
* License: Proprietary
*/
//**START Adding custom fields to product options Inventory tab**
//Display Fields
add_action( 'woocommerce_product_options_inventory_product_data', 'woo_add_custom_general_fields' );
//Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
//Custom fields will be created here...
//Purchase Price Field
woocommerce_wp_text_input(
array(
'id' => '_purchase_price',
'label' => __( 'Purchase price', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Enter the purchase price here.', 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
//Product Link Field
woocommerce_wp_text_input(
array(
'id' => '_product_link',
'label' => __( 'Product link', 'woocommerce' ),
'placeholder' => 'http://',
'desc_tip' => 'true',
'description' => __( 'Enter the product link from the supplier here.', 'woocommerce' )
)
);
echo '</div>';
}
function woo_add_custom_general_fields_save( $post_id ){
//Purchase Price Field
$woocommerce_purchase_price = $_POST['_purchase_price'];
if( !empty( $woocommerce_purchase_price ) )
update_post_meta( $post_id, '_purchase_price', esc_attr( $woocommerce_purchase_price ) );
elseif( empty( $woocommerce_purchase_price ) )
update_post_meta( $post_id, '_purchase_price', NULL );
//Product Link Field
$woocommerce_product_link = $_POST['_product_link'];
if( !empty( $woocommerce_product_link ) )
update_post_meta( $post_id, '_product_link', esc_attr( $woocommerce_product_link ) );
elseif( empty( $woocommerce_product_link ) )
update_post_meta( $post_id, '_product_link', NULL );
}
//**END Adding custom fields to product options Inventory tab**
//**START Plugin Settings Page
//Settings page dropship ids class
class DropshipFields {
public static $counter = 0;
private $dropshipIds;
//Constructor to count how many DropshipFields objects are created
function __construct() {
self::$counter++;
}
//function to register settings
public function ff_dropship_data_scraper_settings() {
$this->dropshipIds = "dropship-ids" . BaseClass::$counter;
return register_setting( 'ff-dropship-data-scraper-settings-group', $this->dropshipIds );
}
//function to display the dropship fields in ff_dropship_data_scraper_settings_page()
public function displayFields() {
}
}
//Eventually I want to put here some code that makes a new object everytime a button is pushed on the settings page
$DropshipFields1 = new DropshipFields();
//Add menu item on admin side
add_action('admin_menu', 'ff_dropship_data_scraper_menu');
function ff_dropship_data_scraper_menu() {
add_menu_page('FF Dropship Data Scraper Settings', 'FF Dropship Data Scraper Settings', 'administrator', 'ff-dropship-data-scraper-settings', 'ff_dropship_data_scraper_settings_page' , 'dashicons-clipboard');
}
add_action( 'admin_init', 'ff_dropship_data_scraper_settings' );
function ff_dropship_data_scraper_settings() {
$DropshipFields1->ff_dropship_data_scraper_settings;
}
function ff_dropship_data_scraper_settings_page() { ?>
<div class="wrap">
<h1>Dropship data</h1>
<form method="post" action="options.php">
<?php settings_fields( 'ff-dropship-data-scraper-settings-group' ); ?>
<?php do_settings_sections( 'ff-dropship-data-scraper-settings-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Dropship IDs</th>
<td><textarea placeholder="Your dropship product ids" name="dropship-ids" rows="5" cols="1000"><?php echo esc_attr( get_option('dropship-ids') ); ?></textarea></td>
</tr>
</table>
<table class="form-table">
<tr valign="top">
<th scope="row">Dropship IDs</th>
<td><textarea placeholder="Your dropship product ids" name="dropship-ids" rows="5" cols="1000"><?php echo esc_attr( get_option('dropship-ids') ); ?></textarea></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php }
//**END Plugin Settings Page
//**START cURL Scraper
foreach($colors as $value) {
//Variables
$url = "https://apibeta.banggood.com/getAccessToken?apiTest=1&apiTest=1app_id=&app_secret=";
$json;
//Initialize
$ch = curl_init();
//Set options
//Url to send the request to
curl_setopt($ch, CURLOPT_URL, $url);
//Return instead of outputting directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//Include header in the output, set to false
curl_setopt($ch, CURLOPT_HEADER, 0);
//Execute the request and fetch the response. Check for errors
$output = curl_exec($ch);
if ($output === FALSE) {
echo 'cURL ERROR: ' . curl_error($ch);
}
//Close and free up the cURL handle
curl_close($ch);
//decode json
$json = json_decode($output, true);
}
//**END cURL Scraper
//**START creating, updating or deleting dropship products
//Put dropship-ids in an array
$text = get_option('dropship-ids');
//explode all separate lines into an array
$textAr = explode("\n", $text);
//trim all lines contained in the array.
$textAr = array_filter($textAr, 'trim');
//Update dropship products from dropship-ids
//Array for the last dropship-ids
$last_ids = [];
//Update function
function update_product_data() {
foreach($textAr as $value) {
if (in_array($value, $last_ids)) {
}
foreach($last_ids as $value) {
if (in_array($value, $textAr)) {
}
}
}
}
//**END creating, updating or deleting dropship products
?>
我打算创建一个Android应用程序,它可以无头登录一个网站,然后在维护登录会话的同时从后续页面中删除一些内容。 我第一次在一个普通的Java项目中使用HtmlUnit,它工作得很好。但后来发现HtmlUnit与Android不兼容。 然后我通过向登录表单发送HTTP“POST”请求来尝试JSoup库。但由于JSoup不支持JavaScript,因此生成的页面无法完全加载。 然后有人建议我看看Se
我对python有点陌生,但我正在尝试制作一个web scraper脚本,它可以在网站上下载所有图片。我正在使用requests和PyQuery,因为许多人在做了一些研究后推荐了它。这就是我现在所拥有的,我不知道该去哪里。 我知道我需要获取img的来源,但在找到img标签后如何做到这一点?此外,我查看了一些htmls的页面源,一些图片存储在他们的数据库中,因此src以“/”开头一些扩展“所以我想知
嗨,我终于能够设置我的webscraper,并将数据导入到我的网页中:) 但是我的网页在端口3001上运行,而网页刮刀在端口8080上运行,我有点困惑,我怎么能设置一个计时器来更新后台的刮刀? Scraper.js 弗雷德里克
用wordpress数据库编写自定义代码创建产品详细页面。 我已经显示了产品的标题,说明,价格,股票等,并与产品属性卡起来。在数据库中,_PRODUCT_Attributes以序列化的方式存储在数据库中的wp_postmeta表中。我也无法从中挖掘出属性。但是我发现,每个具有自己价格的属性值都存储在其他post_id中的wp_postmeta中。 例如,post_id=55的产品具有属性名“siz
因此,我尝试使用jsoup来刮除图像的Reddit,但当我刮除某些子Reddit(如/r/wallpaper)时,我遇到了一个429错误,我想知道如何修复它。完全理解这段代码很糟糕,这是一个很普通的问题,但我对此完全陌生。无论如何:
一面:20-30分钟 1. 自我介绍 2. 对简历上的实习经历深挖。大致业务背景,具体做了什么,哪里觉得有难度,有什么现在觉得可以优化的地方。 3. B站商业化的缺点? 4.B站和油管的对比? 5.常用的APP是什么? 6.反问环节 问了部门业务背景、什么时候给面试结果、面试表现评价? 二面 1. 介绍最近的一段实习经历,我在项目中的角色,是完成任务的人还是主导决策人。 2. SQL代码手撕