Wordpress 产品数据抓取工具/API [英] Wordpress prodruct data scraper/API

查看:52
本文介绍了Wordpress 产品数据抓取工具/API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过抓取或通过 cURL 访问 API 来自动从其他网站获取产品数据.由于我们的网站使用 Wordpress,我正在尝试制作一个插件.我现在正在尝试获取插件设置页面上的字段,以填写网站名称、cURL 的链接格式以及应该导入的产品的 ID.插件的设置页面上会有一个按钮,单击时会再次添加相同的字段.我正在尝试使用带有对象的类,因为我想使用多个网站.我在我们的网站上收到 HTTP 错误 500,所以我认为在我的代码中的//**START 插件设置页面"部分之后做错了什么.如果有人审查我的代码并推动我朝着正确的方向前进,那就太好了:).我对 PHP 也很陌生,所以如果有关于如何使我的代码更具可读性或如何以更好的方式获取产品数据的提示,那就太好了!

亲切的问候,马丁

';//自定义字段将在这里创建...//采购价格字段woocommerce_wp_text_input(大批('id' =>'_购买价格','标签' =>__( '购买价格', 'woocommerce' ),'占位符' =>'','desc_tip' =>'真的','说明' =>__( '在此处输入购买价格.', 'woocommerce' ),'类型' =>'数字','custom_attributes' =>大批('步骤' =>'任何','分钟' =>'0')));//产品链接字段woocommerce_wp_text_input(大批('id' =>'_product_link','标签' =>__( '产品链接', 'woocommerce' ),'占位符' =>'http://','desc_tip' =>'真的','说明' =>__( '在此处输入供应商的产品链接.', 'woocommerce' )));回声'</div>';}函数 woo_add_custom_general_fields_save( $post_id ){//采购价格字段$woocommerce_purchase_price = $_POST['_purchase_price'];if( !empty( $woocommerce_purchase_price ) )update_post_meta( $post_id, '_purchase_price', esc_attr( $woocommerce_purchase_price ) );elseif( 空( $woocommerce_purchase_price ) )update_post_meta( $post_id, '_purchase_price', NULL );//产品链接字段$woocommerce_product_link = $_POST['_product_link'];if( !empty( $woocommerce_product_link ) )update_post_meta( $post_id, '_product_link', esc_attr( $woocommerce_product_link ) );elseif( 空( $woocommerce_product_link ) )update_post_meta( $post_id, '_product_link', NULL );}//**END 将自定义字段添加到产品选项库存选项卡**//**START 插件设置页面//设置页面dropship ids类类 DropshipFields {公共静态 $counter = 0;私人 $dropshipIds;//构造函数计算创建了多少DropshipFields对象函数__构造(){self::$counter++;}//注册设置的函数公共函数 ff_dropship_data_scraper_settings() {$this->dropshipIds = "dropship-ids" .基类::$计数器;返回 register_setting( 'ff-dropship-data-scraper-settings-group', $this->dropshipIds );}//在ff_dropship_data_scraper_settings_page()中显示dropship字段的函数公共函数 displayFields() {}}//最终我想在这里放一些代码,每次在设置页面上按下按钮时都会创建一个新对象$DropshipFields1 = new DropshipFields();//在管理端添加菜单项add_action('admin_menu', 'ff_dropship_data_scraper_menu');函数 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-剪贴板');}add_action('admin_init', 'ff_dropship_data_scraper_settings');函数 ff_dropship_data_scraper_settings() {$DropshipFields1->ff_dropship_data_scraper_settings();}函数 ff_dropship_data_scraper_settings_page() { ?><div class="wrap"><h1>托运数据</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 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><?php submit_button();?></表单>

<?php }//**END 插件设置页面//**启动卷曲刮刀foreach($colors as $value) {//变量$url = "https://apibeta.banggood.com/getAccessToken?apiTest=1&apiTest=1app_id=&app_secret=";$json;//初始化$ch = curl_init();//设置选项//发送请求的地址curl_setopt($ch, CURLOPT_URL, $url);//返回而不是直接输出curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//在输出中包含标题,设置为falsecurl_setopt($ch, CURLOPT_HEADER, 0);//执行请求并获取响应.检查错误$output = curl_exec($ch);如果($output === FALSE){回声'卷曲错误:'.curl_error($ch);}//关闭并释放cURL句柄curl_close($ch);//解码json$json = json_decode($output, true);}//**END 卷曲刮刀//**开始创建、更新或删除直销产品//将dropship-ids放入一个数组中$text = get_option('dropship-ids');//将所有单独的行分解成一个数组$textAr = expand("\n", $text);//修剪包含在数组中的所有行.$textAr = array_filter($textAr, '修剪');//从dropship-ids更新dropship产品//最后一个dropship-id的数组$last_ids = [];//更新函数函数 update_product_data() {foreach($textAr 作为 $value) {如果 (in_array($value, $last_ids)) {}foreach($last_ids 作为 $value) {if (in_array($value, $textAr)) {}}}}//**END 创建、更新或删除直销产品?>

解决方案

试试这个代码

我已将此行 $DropshipFields1->ff_dropship_data_scraper_settings(); 修改为 $DropshipFields1->ff_dropship_data_scraper_settings;

add_menu_page代码

';//自定义字段将在这里创建...//采购价格字段woocommerce_wp_text_input(大批('id' =>'_购买价格','标签' =>__( '购买价格', 'woocommerce' ),'占位符' =>'','desc_tip' =>'真的','说明' =>__( '在此处输入购买价格.', 'woocommerce' ),'类型' =>'数字','custom_attributes' =>大批('步骤' =>'任何','分钟' =>'0')));//产品链接字段woocommerce_wp_text_input(大批('id' =>'_product_link','标签' =>__( '产品链接', 'woocommerce' ),'占位符' =>'http://','desc_tip' =>'真的','说明' =>__( '在此处输入供应商的产品链接.', 'woocommerce' )));回声'</div>';}函数 woo_add_custom_general_fields_save( $post_id ){//采购价格字段$woocommerce_purchase_price = $_POST['_purchase_price'];if( !empty( $woocommerce_purchase_price ) )update_post_meta( $post_id, '_purchase_price', esc_attr( $woocommerce_purchase_price ) );elseif( 空( $woocommerce_purchase_price ) )update_post_meta( $post_id, '_purchase_price', NULL );//产品链接字段$woocommerce_product_link = $_POST['_product_link'];if( !empty( $woocommerce_product_link ) )update_post_meta( $post_id, '_product_link', esc_attr( $woocommerce_product_link ) );elseif( 空( $woocommerce_product_link ) )update_post_meta( $post_id, '_product_link', NULL );}//**END 将自定义字段添加到产品选项库存选项卡**//**START 插件设置页面//设置页面dropship ids类类 DropshipFields {公共静态 $counter = 0;私人 $dropshipIds;//构造函数计算创建了多少DropshipFields对象函数__构造(){self::$counter++;}//注册设置的函数公共函数 ff_dropship_data_scraper_settings() {$this->dropshipIds = "dropship-ids" .基类::$计数器;返回 register_setting( 'ff-dropship-data-scraper-settings-group', $this->dropshipIds );}//在ff_dropship_data_scraper_settings_page()中显示dropship字段的函数公共函数 displayFields() {}}//最终我想在这里放一些代码,每次在设置页面上按下按钮时都会创建一个新对象$DropshipFields1 = new DropshipFields();//在管理端添加菜单项add_action('admin_menu', 'ff_dropship_data_scraper_menu');函数 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');函数 ff_dropship_data_scraper_settings() {$DropshipFields1->ff_dropship_data_scraper_settings;}函数 ff_dropship_data_scraper_settings_page() { ?><div class="wrap"><h1>托运数据</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 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><?php submit_button();?></表单>

<?php }//**END 插件设置页面//**启动卷曲刮刀foreach($colors as $value) {//变量$url = "https://apibeta.banggood.com/getAccessToken?apiTest=1&apiTest=1app_id=&app_secret=";$json;//初始化$ch = curl_init();//设置选项//发送请求的地址curl_setopt($ch, CURLOPT_URL, $url);//返回而不是直接输出curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//在输出中包含标题,设置为falsecurl_setopt($ch, CURLOPT_HEADER, 0);//执行请求并获取响应.检查错误$output = curl_exec($ch);如果($output === FALSE){回声'卷曲错误:'.curl_error($ch);}//关闭并释放cURL句柄curl_close($ch);//解码json$json = json_decode($output, true);}//**END 卷曲刮刀//**开始创建、更新或删除直销产品//将dropship-ids放入一个数组中$text = get_option('dropship-ids');//将所有单独的行分解成一个数组$textAr = expand("\n", $text);//修剪包含在数组中的所有行.$textAr = array_filter($textAr, '修剪');//从dropship-ids更新dropship产品//最后一个dropship-id的数组$last_ids = [];//更新函数函数 update_product_data() {foreach($textAr 作为 $value) {如果 (in_array($value, $last_ids)) {}foreach($last_ids 作为 $value) {if (in_array($value, $textAr)) {}}}}//**END 创建、更新或删除直销产品?>

I want to automatically get product data from other websites either by scraping it or by accessing an API with cURL. Since our website uses Wordpress, I am trying to make a plugin. I am now trying to get fields on the plugin's settings page to fill in the name of the website, the link format for cURL and the IDs of the products that should be imported. There will be a button on the plugin's settings page which adds the same fields another time when clicked on. I am trying to use a class with objects, since I want use multiple websites. I am getting HTTP Error 500 on our website, so I think am doing something wrong after the "//**START Plugin Settings Page" part in my code. It would be great if someone reviews my code and gives me a push in the right direction :). I am also pretty new to PHP so if there are tips on how I can make my code more readable or how I can get the product data in a better way, that would be great!

Kind regards, Martijn

<?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



?>

解决方案

Try this code

I have modified this line $DropshipFields1->ff_dropship_data_scraper_settings(); to $DropshipFields1->ff_dropship_data_scraper_settings;

and add_menu_page code

<?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



?>

这篇关于Wordpress 产品数据抓取工具/API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆