wp_remote_request getting alsways 403 Error - wordpress

im trying to get the xml but i always get a 403 error. But with postman its working fine.
i dont think i did something wrong with the authorization but maybe im wrong.
this code is in the functions.php and im calling it with https://localhost/wordpress/wp-admin/admin-ajax.php?action=nopriv_search_mobileads
add_action('wp_ajax_nopriv_search_mobileads', 'get_mobileads_from_api');
add_action('wp_ajax_search_mobileads', 'get_mobileads_from_api');
function get_mobileads_from_api() {
$url = ('https://services.mobile.de/search-api/search?');
$wp_request_headers = array(
'Host' => 'https://services.mobile.de',
'Accept' => 'application/xml',
'Authorization' => 'Basic '.base64_encode('id:pw')
);
$results = wp_remote_request(
$url,
array(
'method' => 'GET',
'headers' => $wp_request_headers
)
);
print_r($results);
}
output:
Array ( [headers] => Requests_Utility_CaseInsensitiveDictionary Object ( [data:protected] => Array ( ) ) [body] => [response] => Array ( [code] => 403 [message] => Forbidden ) [cookies] => Array ( ) [filename] => [http_response] => WP_HTTP_Requests_Response Object ( [response:protected] => Requests_Response Object ( [body] => [raw] => HTTP/1.1 403 [headers] => Requests_Response_Headers Object ( [data:protected] => Array ( ) ) [status_code] => 403 [protocol_version] => 1.1 [success] => [redirects] => 0 [url] => https://services.mobile.de/search-api/search? [history] => Array ( ) [cookies] => Requests_Cookie_Jar Object ( [cookies:protected] => Array ( ) ) ) [filename:protected] => [data] => [headers] => [status] => ) ) 0

Unless the actual username is "id" and the actual password is "pw" that's not going to work.

found a way.
add_action('wp_ajax_nopriv_get_ads_from_api','get_ads_from_api');
add_action('wp_ajax_get_ads_from_api','get_ads_from_api');
function get_ads_from_api(){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://services.mobile.de/search-api/search?',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Authorization: Basic ***'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
}

Related

WordPress - metadata field empty on local server but not on production server

I created a metadata field named _metafield_gallery in a metabox.
On my production server the _metafield_gallery field contains datas when I display the array of the posts (query with get_posts).
After exporting the database from the production server to the local server when I try to display array of the posts datas with get_posts query, _metafield_gallery is empty.
The datas are present in the exported .sql file yet.
It's an authentification problem ?
register_post_meta(
$post_type,
'_metafield_gallery',
array(
'type' => 'array',
'single' => true,
'show_in_rest' => array(
'schema' => array(
'items' => array(
'type' => 'object',
'properties' => array(
'id' => array(
'type' => 'number',
),
'title' => array(
'type' => 'string',
),
'url' => array(
'type' => 'string',
),
)
),
),
),
'auth_callback' => function() {
return current_user_can( 'edit_posts' );
}
)
);
Production server :
[_metafield_gallery] => Array (
[0] => Array(
[id] => 21468
[title] => 11
[url] => https://mysite.fr/content/uploads/2022/07/11.png
)
)
Local server :
[_metafield_gallery] =>

How can I filter records in custom post type list in admin based on user_id that passed as a query string parameter

Is there any option that I can further filter post list for a custom post type in admin based on user_id if the same has been passed as a query string like http://album.multibaselocal.com/wp-admin/edit.php?post_type=albums&user_id=31 ?
Here is what I want to accomplish:
I have a custom post type called album which is used by site users to upload Albums to the system. By default when I go to the list page all albums come up fine. In user list page, I have added a custom column which shows a count of Published albums by each user which links back to album list page with a query string user_id like above.
I want to filter the albums by the user_id I have passed. Is this possible?
I am trying this:
add_filter( 'parse_query', 'filter_albums_further_if_user_id_found');
function filter_albums_further_if_user_id_found($query) {
if( isset($_GET['user_id']) && !empty($_GET['user_id'])) {
$user_id = $_GET['user_id'];
$requery = array(
'post_type' => 'albums',
'post_author' => $user_id,
'status' => 'publish',
'posts_per_page' => -1,
);
// What to do here?
}
}
I have lost here completely. Not an experienced WP developer!
If I have not understood it all wrong, because the hook I am using is called parse_query, I have to somehow parse the query again and filter the existing result using user_id. But don't know where and how.
EDIT (Query output):
WP_Query Object
(
[query] => Array
(
[post_type] => albums
[posts_per_page] => 20
)
[query_vars] => Array
(
[post_type] => albums
[posts_per_page] => 20
[error] =>
[m] =>
[p] => 0
[post_parent] =>
[subpost] =>
[subpost_id] =>
[attachment] =>
[attachment_id] => 0
[name] =>
[static] =>
[pagename] =>
[page_id] => 0
[second] =>
[minute] =>
[hour] =>
[day] => 0
[monthnum] => 0
[year] => 0
[w] => 0
[category_name] =>
[tag] =>
[cat] =>
[tag_id] =>
[author] =>
[author_name] =>
[feed] =>
[tb] =>
[paged] => 0
[meta_key] => user_id
[meta_value] => 31
[preview] =>
[s] =>
[sentence] =>
[title] =>
[fields] =>
[menu_order] =>
[embed] =>
[category__in] => Array
(
)
[category__not_in] => Array
(
)
[category__and] => Array
(
)
[post__in] => Array
(
)
[post__not_in] => Array
(
)
[post_name__in] => Array
(
)
[tag__in] => Array
(
)
[tag__not_in] => Array
(
)
[tag__and] => Array
(
)
[tag_slug__in] => Array
(
)
[tag_slug__and] => Array
(
)
[post_parent__in] => Array
(
)
[post_parent__not_in] => Array
(
)
[author__in] => Array
(
)
[author__not_in] => Array
(
)
)
[tax_query] => WP_Tax_Query Object
(
[queries] => Array
(
)
[relation] => AND
[table_aliases:protected] => Array
(
)
[queried_terms] => Array
(
)
[primary_table] =>
[primary_id_column] =>
)
[meta_query] =>
[date_query] =>
[post_count] => 0
[current_post] => -1
[in_the_loop] =>
[comment_count] => 0
[current_comment] => -1
[found_posts] => 0
[max_num_pages] => 0
[max_num_comment_pages] => 0
[is_single] =>
[is_preview] =>
[is_page] =>
[is_archive] => 1
[is_date] =>
[is_year] =>
[is_month] =>
[is_day] =>
[is_time] =>
[is_author] =>
[is_category] =>
[is_tag] =>
[is_tax] =>
[is_search] =>
[is_feed] =>
[is_comment_feed] =>
[is_trackback] =>
[is_home] =>
[is_404] =>
[is_embed] =>
[is_paged] =>
[is_admin] => 1
[is_attachment] =>
[is_singular] =>
[is_robots] =>
[is_posts_page] =>
[is_post_type_archive] => 1
[query_vars_hash:WP_Query:private] => 1c0b485390fc9607d8008a14177bd63d
[query_vars_changed:WP_Query:private] =>
[thumbnails_cached] =>
[stopwords:WP_Query:private] =>
[compat_fields:WP_Query:private] => Array
(
[0] => query_vars_hash
[1] => query_vars_changed
)
[compat_methods:WP_Query:private] => Array
(
[0] => init_query_flags
[1] => parse_tax_query
)
)
EDIT (Function body presently using):
add_action( 'pre_get_posts', 'filter_albums_further_if_user_id_found');
function filter_albums_further_if_user_id_found($query) {
if(!is_admin())
return;
if( isset($_GET['user_id']) && !empty($_GET['user_id']) ) {
$user_id = $_GET['user_id'];
$query->set('meta_key', 'user_id');
$query->set('meta_value', $user_id);
}
echo '<pre>'; print_r($query); echo '</pre>'; die();
}
This reminded me of a similar problem I had a couple weeks ago. I was wanting to adjust the 'orderby' part of the query, but I believe you can accomplish your task with the same action. Try using the 'pre_get_posts' action instead of that filter. I haven't tested this, so let me know if it works!
CORRECT ANSWER (EDITED after user input):
function user_id_filter( $query ) {
if ( ! is_admin() && !$query->is_main_query() )
return;
if( isset($_GET['user_id']) && !empty($_GET['user_id'])) {
$user_id = $_GET['user_id'];
$query->set( 'author', $user_id );
}
//Debug if necessary
//echo '<pre>'; print_r($query); echo '</pre>'; die();
}
add_action( 'pre_get_posts', 'user_id_filter', 500000000 );
I used this as a reference. https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

Having trouble with Symfony2 uploading file from external program - 'mimeType' => 'application/octet-stream'

I have a Symfony2 endpoint that needs to accept a post request with a few text parameters and a zip file delivered by a 3rd party program. For quick development I started out by just using a Chrome POST plugin to spoof sending the form data. I was able to retrieve the file just fine when using that method.
However, when I switched to the 3rd party Windows application for uploading reports, I'm no longer able to retrieve the file. Instead of figuring out that it's a zip file, I now get mimeType' => 'application/octet-stream', and the file size is 0.
I read here in the following link that I needed to make sure my post_max_size and upload_max_filesize were large enough to handle the file. Can't upload image with mime-type "application/octet-stream" in Symfony2
I didn't want to mess around, so I set it to 100mb, but still no luck.
Here is a dump of the Request:
[2016-08-24 10:15:48] app.INFO: Request dump: Symfony\Component\HttpFoundation\Request::__set_state(array( 'attributes' => Symfony\Component\HttpFoundation\ParameterBag::__set_state(array( 'parameters' => array ( '_format' => 'xml', '_controller' => 'Company\\Bundle\\CompanyBundle\\Controller\\DefaultController::cloudTransferReport', '_route' => 'hg_cloudtransfer_report', '_route_params' => array ( '_format' => 'xml', ), '_method' => Sensio\Bundle\FrameworkExtraBundle\Configuration\Method::__set_state(array( 'methods' => array ( 0 => 'POST', 1 => 'GET', ), )), ), )), 'request' => Symfony\Component\HttpFoundation\ParameterBag::__set_state(array( 'parameters' => array ( 'submitter' => 'hgis', 'version' => '5.1.26.0', 'userName' => 'admin', 'password' => 'kitten', ), )), 'query' => Symfony\Component\HttpFoundation\ParameterBag::__set_state(array( 'parameters' => array ( ), )), 'server' => Symfony\Component\HttpFoundation\ServerBag::__set_state(array( 'parameters' => array ( 'REDIRECT_HTTPS' => 'on', 'REDIRECT_SSL_TLS_SNI' => 'example.com', 'REDIRECT_STATUS' => '200', 'HTTPS' => 'on', 'SSL_TLS_SNI' => 'example.com', 'CONTENT_TYPE' => 'multipart/form-data, boundary=AaBbCcDdHhGg40', 'HTTP_USER_AGENT' => 'Some software v 5.1.26.0', 'HTTP_HOST' => 'example.com', 'CONTENT_LENGTH' => '3592731', 'HTTP_CACHE_CONTROL' => 'no-cache', 'PATH' => '/usr/bin:/bin:/usr/sbin:/sbin', 'SERVER_SIGNATURE' => '', 'SERVER_SOFTWARE' => 'Apache/2.4.18 (Unix) PHP/5.5.36 LibreSSL/2.2.7', 'SERVER_NAME' => 'example.com', 'SERVER_ADDR' => '192.168.1.100', 'SERVER_PORT' => '443', 'REMOTE_ADDR' => '192.168.1.223', 'DOCUMENT_ROOT' => '/Users/user/Documents/symphony/cpsrecall/web', 'REQUEST_SCHEME' => 'https', 'CONTEXT_PREFIX' => '', 'CONTEXT_DOCUMENT_ROOT' => '/Users/user/Documents/symphony/cpsrecall/web', 'SERVER_ADMIN' => 'chris#yourcontactpoint.com', 'SCRIPT_FILENAME' => '/Users/user/Documents/symphony/cpsrecall/web/app.php', 'REMOTE_PORT' => '51248', 'REDIRECT_URL' => '/hg/cloudtransfer/upload-report', 'GATEWAY_INTERFACE' => 'CGI/1.1', 'SERVER_PROTOCOL' => 'HTTP/1.1', 'REQUEST_METHOD' => 'POST', 'QUERY_STRING' => '', 'REQUEST_URI' => '/hg/cloudtransfer/upload-report', 'SCRIPT_NAME' => '/app.php', 'PHP_SELF' => '/app.php', 'REQUEST_TIME_FLOAT' => 1472055346.6129999, 'REQUEST_TIME' => 1472055346, ), )), 'files' => Symfony\Component\HttpFoundation\FileBag::__set_state(array( 'parameters' => array ( 'file1' => Symfony\Component\HttpFoundation\File\UploadedFile::__set_state(array( 'test' => false, 'originalName' => '00000003-1.zip', 'mimeType' => 'application/octet-stream', 'size' => 0, 'error' => 1, )), ), )), 'cookies' => Symfony\Component\HttpFoundation\ParameterBag::__set_state(array( 'parameters' => array ( ), )), 'headers' => Symfony\Component\HttpFoundation\HeaderBag::__set_state(array( 'headers' => array ( 'content-type' => array ( 0 => 'multipart/form-data, boundary=AaBbCcDdHhGg40', ), 'user-agent' => array ( 0 => 'Some software v 5.1.26.0', ), 'host' => array ( 0 => 'example.com', ), 'content-length' => array ( 0 => '3592731', ), 'cache-control' => array ( 0 => 'no-cache', ), 'x-php-ob-level' => array ( 0 => 1, ), ), 'cacheControl' => array ( 'no-cache' => true, ), )), 'content' => '', 'languages' => NULL, 'charsets' => NULL, 'encodings' => NULL, 'acceptableContentTypes' => NULL, 'pathInfo' => '/hg/cloudtransfer/upload-report', 'requestUri' => '/hg/cloudtransfer/upload-report', 'baseUrl' => '', 'basePath' => NULL, 'method' => 'POST', 'format' => NULL, 'session' => Symfony\Component\HttpFoundation\Session\Session::__set_state(array( 'storage' => Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage::__set_state(array( 'bags' => array ( 'attributes' => Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag::__set_state(array( 'name' => 'attributes', 'storageKey' => '_sf2_attributes', 'attributes' => array ( ), )), 'flashes' => Symfony\Component\HttpFoundation\Session\Flash\FlashBag::__set_state(array( 'name' => 'flashes', 'flashes' => array ( ), 'storageKey' => '_sf2_flashes', )), ), 'started' => false, 'closed' => false, 'saveHandler' => Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy::__set_state(array( 'handler' => SessionHandler::__set_state(array( )), 'wrapper' => true, 'active' => false, 'saveHandlerName' => 'files', )), 'metadataBag' => Symfony\Component\HttpFoundation\Session\Storage\MetadataBag::__set_state(array( 'name' => '__metadata', 'storageKey' => '_sf2_meta', 'meta' => array ( 'c' => 0, 'u' => 0, 'l' => 0, ), 'lastUsed' => NULL, 'updateThreshold' => '0', )), )), 'flashName' => 'flashes', 'attributeName' => 'attributes', )), 'locale' => NULL, 'defaultLocale' => 'en', )) [] []
and for those who don't want to read that big long line, here's just the FileBag:
[2016-08-24 10:15:48] app.INFO: file1dump: Symfony\Component\HttpFoundation\File\UploadedFile::__set_state(array( 'test' => false, 'originalName' => '00000003-1.zip', 'mimeType' => 'application/octet-stream', 'size' => 0, 'error' => 1, )) [] []
Here is my code up that should grab the file, but perhaps I'm going about it wrong:
public function cloudTransferReport(Request $request)
{
$logger = $this->get('logger');
$em = $this->getDoctrine()->getManager();
$data = $request->request;
$files = $request->files;
$submitter = $data->get('submitter');
$version = $data->get('version');
$memberName = $data->get('userName');
$password = $data->get('password');
$file1 = $files->get('file1');
$logger->info("file string: " . var_export($stuff, true));
$logger->info("file1dump: " . var_export($file1, true));
If you need more code from that controller let me know, but I thought I should at least be able to have the file in memory at this point.
Ok Mac users who run into an issue like this, especially those who have used homebrew, and may or may not have more than one version of PHP installed.
You may have more than one php.ini file on your computer. I ran the php --ini, and it came up with Loaded Configuration File: /usr/local/etc/php/5.6/php.ini. IT WAS WRONG!
Ends up the real php.ini file that Apache was using was in /etc/php.ini, so please, please, please don't waste a whole day banging your head against the keyboard. Make sure you're in the file that Apache is actually using! That is all. I'm going to crawl under my desk and cry now.

Schedule email using wpMandrill to Wordpress

Is that possible to schedule a message using wpMandrill plugin?
I saw in this post that we can use mandrill_payload filter to change anything in the structure (following Mandrill's API , /messages/send).
How can I change the send_at parameter so that I can schedule e-mails to be sent.
Would it be something like this:
function customFilterSendAt($send_at)
{
$send_at = "2016-01-23 14:00:00";
return $send_at;
}
add_filter('mandrill_payload', 'customFilterSendAt');
Then
wp_mail($email_adress, $subj, $body );
?
I found that it is possible to schedule emails using wpMandrill. This link (check aaroneight comments) helped me.
To schedule your email using wpMandrill:
$message = array(
'subject' => $subj,
'from_name' => 'From Name',
'from_email' => 'from_email#example.com',
'to' => 'to_email#example.com',
'html' => $body,
'async' => false,
'ip_pool' => null,
'send_at' => '2016-02-24 19:45:00'
);
$sendmessage = wpMandrill::sendEmail($message);
Debugging:
echo '<pre>'.print_r($sendmessage,true).'</pre>';
Output example:
Array
(
[0] => Array
(
[email] => to_email#example.com
[status] => scheduled
[_id] => db835dfe43cd5d67b3743a30e184f84d
[reject_reason] =>
)
)
At this time, scheduled emails may only be managed via Mandrill's API, so you won't find them within Mandrill's Dashboard.
to list your scheduled emails you can use:
$url = 'https://mandrillapp.com/api/1.0/messages/list-scheduled.json';
$key = 'Your API key';
//$to = ''; // optional
$args = array(
'body' => array(
'key' => $key
)
);
$results = wp_remote_post( $url, $args );
$results = json_decode($results['body']);
Output example:
Array
(
[0] => stdClass Object
(
[_id] => 1d0xe54f3b759a1153b7a53g3321f4b6
[created_at] => 2016-02-24 19:30:13
[send_at] => 2016-02-24 19:42:00
[from_email] => from_email#example.com
[to] => to_email#example.com
[subject] => Email Subject
)
[1] => stdClass Object
(
[_id] => 1272e526f6924ba096d23146e2dxad4c
[created_at] => 2016-02-24 19:31:12
[send_at] => 2016-02-24 19:45:00
[from_email] => from_email#example.com
[to] => to_email#example.com
[subject] => Email Subject
)
)

Error: One or more parameter values were invalid--DynamoDb

I am trying to update a table in DynamoDb with the following code..
$response = $client->updateItem(array(
"TableName" => "PlayerInfo",
"Key" => array(
"PlayerId" => array('N' => '201503261435580358849074082'),
),
"AttributeUpdates" => array(
'PlayerPrice' => array(
'N' => '5'
),
),
"ReturnValues" => \Aws\DynamoDb\Enum\ReturnValue::ALL_NEW
));
print_r($response);
However, an error interrupts its execution. It says:
One or more parameter values were invalid: Only DELETE action is allowed
when no attribute value is specified.
Could anybody help me with this issue?
Looks like the for format of the request was missing the 'Action' and 'Value' parameters. E.g. the following is working for me:
$response = $client->updateItem(array(
"TableName" => "PlayerInfo",
"Key" => array(
"PlayerId" => array('N' => '201503261435580358849074082'),
),
"ReturnValues" => \Aws\DynamoDb\Enum\ReturnValue::ALL_NEW,
"AttributeUpdates" => array(
'PlayerPrice' => array(
'Action' => \Aws\DynamoDb\Enum\AttributeAction::PUT,
'Value' => array('N' => '5'),
)
)
));
print_r($response);
You can also use an UpdateExpression to achieve the same effect (UpdateExpressions also provide greater flexibility than AttributeUpdates so they are generally recommended):
$response = $client->updateItem(array(
"TableName" => "PlayerInfo",
"Key" => array(
"PlayerId" => array('N' => '201503261435580358849074082'),
),
"ReturnValues" => \Aws\DynamoDb\Enum\ReturnValue::ALL_NEW,
"UpdateExpression" => "SET #pp = :val",
"ExpressionAttributeNames" => array(
"#pp" => "PlayerPrice",
),
"ExpressionAttributeValues" => array(
':val' => array('N' => '5')
)
));
print_r($response);

Resources