I want to get a URL which is coming from a React rout.
URL is like: http://localhost:3001/users/[member_username]
In which member_username will be dynamic as per the user name.
So for example if a url hit like http://localhost:3001/users/gray then a variable will get gray or url hit like http://localhost:3001/users/john then a variable will get john
Now i need to get that url into WordPress php file to create an API.
I have tried this solution but did not getting the exact solution that is fit to my problem.
Here is my API code that in which i need to get URL.
function get_user_id(){
$server_name = $_SERVER['SERVER_NAME'];
$user_name = basename("http://".$server_name."/users/admin");
// For now i have set the username as static, so it should be a dynamic as user hit the url.
$users = get_user_by('login', $user_name);
if( !empty($users) ){
foreach ($users as $key => $user) {
$user_name = new stdClass();
$user_name->id = $user->ID;
$user_name->user_login = $user->user_login;
$user_name->user_nicename = $user->user_nicename;
$user_name->user_email = $user->user_email;
return $user_name;
}
}
}
/*
*
* Add action endpoint building
*
*/
add_action( 'rest_api_init', function () {
register_rest_route( 'rest-endpoints/v1', '/userid', array(
'methods' => 'GET',
'callback' => 'get_user_id'
));
});
Could you let me know why that solution not fit for your problem?
seems is ok to get the username.
however, maybe you can try this
<?php
$url = parse_url('http://localhost:3001/users/yourname',PHP_URL_PATH);
$url = str_replace("/users/","",$url);
echo $url;
?>
You can use:
$url_path = parse_url('http://localhost:3001/users/a13/abc',PHP_URL_PATH);
$url = preg_replace("/\/(?:[^\/]*\/)*/","",$url_path);
echo $url;
output:
abc
demo: https://regex101.com/r/19x7ut/1/
or this regex:
\/(?:[^\/\s]*\/)*
demo: https://regex101.com/r/19x7ut/3/
both should work fine.
Related
New dev here so I am sorry if this is a simple question or something like that but haven't been able to find exactly what I am looking for (it totally could be that I am not asking the question correctly either) Basically I have a paginated list coming back from an api and I am not sure what the logic would be to be able to cycle through those pages. Here is the code that I have so far that works perfectly for the first page lol.
public function handle()
{
//This is the artisan command that runs on a timer and gets all the ticket and update fields that are needed and saves them to the database.
$tickets = $this->pullTicketSummary();
collect($tickets['data'])
->each(function ($currTicket) {
$ticketRow = Tickets::query()->firstOrCreate(['ticket_id' => $currTicket['id']]);
$ticketRow->status_id = $currTicket['status']['id'];
$ticketRow->category_id = $currTicket['category']['id'];
$ticketRow->user_id = $currTicket['assigned_to']['id'];
$ticketRow->jira_issue_id = $currTicket['jira_issue_id'];
$ticketRow->save();
collect($currTicket['updates'])->each(function ($update) use ($currTicket){
$updateRow = Update::query()->firstOrCreate(['update_id' => $update['update_id']]);
$updateRow->ticket_id = $currTicket['id'];
$updateRow->assignee_change = $update['assignee_change'];
});
});
Log::info('All tickets and updates were pulled successfully');
}
protected function pullTicketSummary()
{ //Function makes the guzzle request and returns the response from the happyfox api
$client = new Client();
$request = $client->get('https://happyfox.com/api/1.1/json/tickets/?size=50&page=1',
['auth' => ['N/A']);
$response = json_decode($request->getBody()->getContents(), true);
return $response;
}
With the fact that I am new if this is something that has been answered before that I missed please just shoot me over a link or if you are aware of any documentation that would help me get to the answer on my own that would be awesome! thanks!
Update your function to use the page number:
protected function pullTicketSummary($page)
{ //Function makes the guzzle request and returns the response from the happyfox api
$client = new Client();
$request = $client->get('https://happyfox.com/api/1.1/json/tickets/?size=50&page='.$page,
['auth' => ['N/A']);
$response = json_decode($request->getBody()->getContents(), true);
return $response;
}
//new function only to save the data from tickets variable. Necessary to reuse.
public function saveTicketsOrSomething($tickets)
{
collect($tickets['data'])
->each(function ($currTicket) {
$ticketRow = Tickets::query()->firstOrCreate(['ticket_id' => $currTicket['id']]);
$ticketRow->status_id = $currTicket['status']['id'];
$ticketRow->category_id = $currTicket['category']['id'];
$ticketRow->user_id = $currTicket['assigned_to']['id'];
$ticketRow->jira_issue_id = $currTicket['jira_issue_id'];
$ticketRow->save();
collect($currTicket['updates'])->each(function ($update) use ($currTicket){
$updateRow = Update::query()->firstOrCreate(['update_id' => $update['update_id']]);
$updateRow->ticket_id = $currTicket['id'];
$updateRow->assignee_change = $update['assignee_change'];
});
});
}
Then iterate until you finish all the pages:
$tickets = $this->pullTicketSummary(1); //first time
$numPages = $tickets['numPages']; //update here to get the actual value of number of pages
$this->saveTicketsOrSomething($tickets);
for ($i = 2; $i < $numPages; $i++) { //start on 2, cause we already have fetched page 1
$tickets = $this->pullTicketSummary($i); //other pages
$this->saveTicketsOrSomething($tickets);
}
Log::info('All tickets and updates were pulled successfully');
This code is working fine only in this function but when i get session in other functions then it return an empty response.
$id = '123';
$this->session->set_userdata('id','$id');
$get_id = $this->session->userdata('id');
print_r($get_id);
Get data in other function like.
$get_id = $this->session->userdata('id');
print_r($get_id);
which is return an empty response.
If anyone has any suggestions it would be very much apppreciated!
Thank you
Nearly, try this.
$id = array( '123' ); <-------
$this->session->set_userdata( 'id', $id ); <--------
$get_id = $this->session->userdata('id');
print_r($get_id);
I am currently developing a WordPress theme based on _s because the project is pretty different than average WordPress projects.
I started a few days ago and have done designing and now I am stuck in one place for hours.
I am trying to create a function to generate video id from Youtube link (a custom field does the job for 'video' post type).
The output is something like this: a0uGWc170Jc.
The code I used is below:
$id = get_post_meta($post->ID, 'video_option_youtube-link', true);
$id = explode('?v=', $id);
if (empty($id[1])) {
$id = explode('/v/', $id);
}
$id = explode("&", $id[1]);
$id = $id[0];
echo $id;
This code works well only when I add it inside of WP Query. I tried to make a function and inserted this function to theme's function.php file and tried to echo inside WP_Query but it returns php error. The function I made is:
function my_videoid() {
$id = get_post_meta($post->ID, 'video_option_youtube-link', true);
$id = explode('?v=', $id);
if (empty($id[1])) {
$id = explode('/v/', $id);
}
$id = explode("&", $id[1]);
$id = $id[0];
return $id;
}
(also tried to echo instead of return)
So, what am I doing wrong?
I think you have missed the global $post, $wpdb;
Please try it.
I am trying to retrieve the project names for all the active projects. I was following the documentation and code given over hereHarvest API. But it throws an error Call to undefined method HarvestAPI::getActiveProjects .Here is the code which I have written
<?php
require_once(dirname(__FILE__) . '/HarvestAPI.php');
/* Register Auto Loader */
spl_autoload_register(array('HarvestAPI', 'autoload'));
$api = new HarvestAPI();
$api->setUser( $user );
$api->setPassword( $password );
$api->setAccount($account );
$api->setRetryMode( HarvestAPI::RETRY );
$api->setSSL(true);
$result = $api->getActiveProjects();
foreach( $result->data as $project ) {
echo $project->name;
}
?>
P.S. I have administrator access for the account. So, I should be able to view all project titles
Look into HarvestAPI.php you will see that there is no method called getActiveProjects
Use this one
public function getProjects( $updated_since = null )
{
$url = "projects" . $this->appendUpdatedSinceParam( $updated_since );
return $this->performGET( $url, true );
}
Am trying to pass some data from one function to another when i set the data into session and print the session data i get the correct data, but whe trying to use the data in another function i get the word "Assets" i dont know where this word come from. Session library is auto loaded.Any help please.
These are my codes:
First function:
$client_id = $this->uri->segment(3);
$sess_array = array(
'cl_id' => $client_d,
'selected_client'=>TRUE,
);
$this->session->set_userdata('selected_client',$sess_array);
Second function:
$client_sess = $this->session->userdata('selected_client');
$client_id= $client_sess['cl_id'];
Try this i think this will give you some idea.
function one(){
$client_id = $this->uri->segment(3);
$sess_array = array(
'cl_id' => $client_d,
'selected_client'=>TRUE,
);
$this->two($sess_array);
}
function two($id){
$client_id= $id;
}
Here is what the Model looks like:
function getResponse($gettingresponse)
{
$enrollresponse=$gettingresponse['sendresponse'];
return $enrollresponse;
}
The Controller is as follows:
public function Register()
{
$this->load->view('firstview');
$this->load->view('secondview');
if($_POST) {
$gettingresponse=array(
'sendresponse'=>$_POST['source'],
'receiverresponse'=>$_POST['destination']
);
$registration_confirm=$this->systemModel->responselogin($gettingresponse);
$resposeflag=$this->systemModel->getEmail($gettingresponse);
$data['resposeflag']=$gettingresponsevalue;
if($registration_confirm){
$this->token($data);
}
}
$this->load->view('thirdview');
}
public function token($data=array())
{
$this->load->view('firstview');
$data['resposeflag'];
$this->load->view('token',$data);
$this->load->view('thirdview');
}
The following View shows the data that has been passed between the functions of the Controller.
<?php
echo form_input(array('name'=>'source','readonly'=>'true','value'=>$resposeflag));
?>