admin管理员组

文章数量:1122832

I have a PHP(Laravel) application that pushes data to WooCommerce through the WooCommerce REST API, everything works except for images. I was able to pin this down to wp_safe_remote_get() and $args['reject_unsafe_urls'].

I had found a way around this, but I cannot recall where I found it. I seem to remember a hook in functions.php that turned off this feature. It wasn't recommended, but it is the only recourse.

Can anyone help me out? Or does anyone have another solution? I'm only pushing URLs from an application I made from the same server.

Code Example from External Application

$this->woocommerce->post('products', ['product' => $book_data_array])

where $this->woocommerce is an instance of the WooCommerce API.

and $book_data_array is an array of data. For an example of the data arra, see: '.html#create-a-product'

The only thing that doesn't work is images coming from the same server, which is a WordPress issue and has been confirmed as much by a WooCommerce dev. WordPress doesn't allow downloads from the same origin without an override.

I have a PHP(Laravel) application that pushes data to WooCommerce through the WooCommerce REST API, everything works except for images. I was able to pin this down to wp_safe_remote_get() and $args['reject_unsafe_urls'].

I had found a way around this, but I cannot recall where I found it. I seem to remember a hook in functions.php that turned off this feature. It wasn't recommended, but it is the only recourse.

Can anyone help me out? Or does anyone have another solution? I'm only pushing URLs from an application I made from the same server.

Code Example from External Application

$this->woocommerce->post('products', ['product' => $book_data_array])

where $this->woocommerce is an instance of the WooCommerce API.

and $book_data_array is an array of data. For an example of the data arra, see: 'http://woocommerce.github.io/woocommerce-rest-api-docs/wp-api-v1.html#create-a-product'

The only thing that doesn't work is images coming from the same server, which is a WordPress issue and has been confirmed as much by a WooCommerce dev. WordPress doesn't allow downloads from the same origin without an override.

Share Improve this question edited May 4, 2017 at 13:08 nobrandheroes asked May 4, 2017 at 12:13 nobrandheroesnobrandheroes 1136 bronze badges 6
  • Where is wp_safe_remote_get being called? By API I assume you mean the REST API? – Tom J Nowell Commented May 4, 2017 at 12:15
  • 1 wp_safe_remote_get is being called wherever WordPress calls it, I'm not calling it anywhere specifically. My assumption is the WooCommerce REST API is using the native WordPress REST API. I do mean REST API. You assumed correctly. – nobrandheroes Commented May 4, 2017 at 12:23
  • Can we see the code you're using to create posts? Technically this falls under WooCommerce plugin support though, so there's a high chance this is out of scope/offtopic here – Tom J Nowell Commented May 4, 2017 at 12:27
  • 1 Posts are created through the PHP REST API. It isn't a WooCommerce issue, I've gone down that route. It is because WordPress uses wp_safe_remote_get to filter out URLs coming from the same server. – nobrandheroes Commented May 4, 2017 at 12:46
  • I don't understand, are the URLs passed for post meta? Or are they embedded in the content? We really need to see the code that makes the REST API calls, you may disagree but where's the harm? You might get an answer that provides a different way of doing it that bypasses the problem entirely and gets you what you need. It also provides people with a way of replicating the issue and testing out solutions – Tom J Nowell Commented May 4, 2017 at 13:02
 |  Show 1 more comment

2 Answers 2

Reset to default 1

A safer way to archive the same result, since WP 5.1, is to use http_request_reject_unsafe_urls filter:

function performize_turn_off_reject_unsafe_urls($ret, $url) 
{
    //here you can use regex or anything you want to check the url 
    
    if ($url==='https://urltowhitelist')
        $ret = false;

    return $ret;
}

add_filter( 'http_request_reject_unsafe_urls', 'performize_turn_off_reject_unsafe_urls', 10, 2 );

Also, http_request_args use 2 parameters so if you want to use http_request_args you can do it in this way:

function turn_off_reject_unsafe_urls($args, $url) {
 if ($url === 'https://urltowhitelist')
    $args['reject_unsafe_urls'] = false;

    return $args;
}

add_filter( 'http_request_args', 'turn_off_reject_unsafe_urls', 10, 2);

This is the fix I found.

function turn_off_reject_unsafe_urls($args) {
    $args['reject_unsafe_urls'] = false;

    return $args;
}

add_filter( 'http_request_args', 'turn_off_reject_unsafe_urls');

本文标签: imagesBypass wpsaferemoteget()