vendredi 8 avril 2022

Laravel Http Client - How to upload file to linkedin Assets API

I wanted to upload a file using Laravel HTTP Client, but I'm not able to understand how to work or attach media to LinkedIn. And moreover, LinkedIn API does not even give back any response after upload this becomes even harder for me to figure out where I went wrong.

But LinkedIn documentation shows an example using the curl command which I successfully achieved to upload a file and even in the postman I was able to do so by choosing the PUT method and body as a binary file.

Below is LinkedIn Doc under the "Upload the Image" section an example is given for bash.

https://docs.microsoft.com/en-us/linkedin/marketing/integrations/community-management/shares/vector-asset-api?tabs=http#upload-the-image

Below is the small piece of code I'm trying to achieve upload functionality

   Http::attach('file', file_get_contents($request->file('file')), 'perfidious.jpg')
            ->withHeaders([
                'Authorization' => 'Bearer ' . $oauth2_token,
                'Content-Type' => $file->getMimeType(),
            ])->put('https://api.linkedin.com/mediaUpload/C4E22AQFyx5-WPFqU4w/feedshare-uploadedImage/0?ca=vector_feedshare&cn=uploads&m=AQJDUuJEebKdjgAAAYAIF2PvtGz3bIfDzdyIAomflRbj4jD-Z1lcfP-7NQ&app=201094746&sync=1&v=beta&ut=1S4fxf2p45tWc1')
            ->json();

What I know from Laravel Docs is "file_get_contents" method reads file data as a string.

https://laravel.com/docs/9.x/http-client

Please, anyone, help me guide on how to do it as I have very minimal knowledge in PHP and Laravel. Thanks!



via Chebli Mohamed

jeudi 7 avril 2022

in array how to pass query for perfect result

 $arr = str_replace('"','', $request->course_id);
  $arr = str_replace('"','', $request->stream_id);
    $arr = str_replace('[','', $arr);
    $arr = str_replace(']','', $arr);
    $arr = explode(',',$arr);
      $a = array(College::whereIn('course_id',$arr)->get());
     $b = array(College::whereIn('stream_id',$arr)->get());
    
    $result =  array_merge($arr,$a, $b );
     return $result;

enter image description here

This is code i done, but not get exact result which i want, i want to filter like when pass id course id 1 and value BCA then show BCA college list, college is 1 table and course is another table and common id in both table is course id, what is exact problem in this code



via Chebli Mohamed

mercredi 6 avril 2022

cannot run commands in tinkerwell over ssh connection (laravel)

Tinker works just fine when I am ssh'd into my box, but I cannot get it to work in tinkerwell.

I'm trying to connect to my local vagrant box with tinkerwell. It is a vagrant box running in windows: ubuntu 20, laravel 5.7, php7.3.33. Tinkerwell 2.25 in Windows 10. I can connect over ssh, but it errors out whenever I run any command:

enter image description here

In Shell.php line 79: Argument 1 passed to _PhpScoperc223a629f245\Psy\Shell::add() must be an ins tance of _PhpScoperc223a629f245\Symfony\Component\Console\Command\Command, instance of Psy\Command\ParseCommand given, called in phar:///tmp/vagrant_t
inker.phar/vendor/symfony/console/Application.php on line 316

I get the same error whenever I try to run any command. On google I found a thread where someone had the same issue, except they were not connecting over ssh, they were on a mac, and the thread had no resolution in it (https://github.com/beyondcode/tinkerwell-community/issues/215).

I have checked that the correct version of php is in my $PATH, so the default path of php for the php executable that tinkerwell picks seems correct. I have also tried specifying /usr/bin/php but it doesn't change anything.



via Chebli Mohamed

mardi 5 avril 2022

laravel - can not change base URL in Docker + Laravel PHP + Nginx

I try to migrate my backend website (containing API service to mobile apps) from LAMP into docker platform. currently this docker split into 3 parts (Laravel App, database & nginx). sofar, the website launch successfully without error.

However, I need base URL to be like below:

http://backend.example.com/public/

so, if i want to login, url will be http://backend.example.com/public/login, also API url with above format like http://backend.example.com/public/api/v1

What I have tried:

  1. Set APP_URL value in .env to http://backend.example.com/public/
  2. Set below setting in config/app.php to:
'url' => env('APP_URL', 'https://backend.example.com/public'),
'asset_url' => env('ASSET_URL', null)
  1. run php artisan route:clear and php artisan migrate

but still not successfull, evertime i launch web browser, url still stuck to:

http://backend.example.com/

http://backend.example.com/login/

http://backend.example.com/api/v1/

etc

any idea to overcome above problem?

===Additional Note

  1. nginx conf for nginx docker:
server {
    listen 80;
    index index.php index.html;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/public;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
    location / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }
}
  1. original base URL in LAMP are http://backend.example.com/public/, in original i'm using LAMP (Apache+PHP+MySQL) but in current Docker I'm using (Nginx + MySQL + php:7.4-fpm), however because an error, i change something so original base url cannot be achieved anymore...

  2. Reference of This Migration can be found here.



via Chebli Mohamed

How to pass many records through Form in Laravel 8?

The essence of the problem is that I started to learn Laravel and I can not find information on how to pass many fields through the form, followed by validation. Form example:

How to do it right?

`<form class="mt-5" method="post" enctype="multipart/form-data"
    @isset($word)
        action=""
    @else
        action=""
    @endisset
@csrf
@isset($word)
    @method('PUT')
@endisset
<table class="table table-responsive-md">
    <tbody>
        <tr>
            <th>ID</th>
            <th>English</th>
            <th>Transcription</th>
            <th>Russian</th>
            <th>Ukrainian</th>
            <th>Module</th>
            <th>Action</th>
         </tr>
         @for($i = 1; $i < 3; $i++)
            <tr>
                <td></td>
                <td><input type="text" name="eng[]" class="form-control"
                                           value=""></td>
                <td><input type="text" name="transaction[]" class="form-control"
                                           value=""></td>
                <td><input type="text" name="ru[]" class="form-control"
                                           value=""></td>
                <td><input type="text" name="uk[]" class="form-control"
                                           value=""></td>
                <td><input type="text" name="category_id[]" class="form-control"
                                           value=""></td>
                <td></td>
             </tr>
           @endfor
       </tbody>
    </table>
    <a class="btn btn-secondary mt-4" href="">Back</a>
    <button class="btn btn-success  mt-4">Save</button>
</form>`

The output is like this, but I know for sure that this is not right.

^ array:6 [▼ "_token" => "Olp8kMQIFoDP9OOvV5YRihcV3FpKIHofxfYk8W7M" "eng" => array:2 [▶] "transaction" => array:2 [▶] "ru" => array:2 [▼ 1 => "www" 2 => "qqq" ] "uk" => array:2 [▶] "category_id" => array:2 [▶] ]



via Chebli Mohamed

lundi 4 avril 2022

Change .htaccess directory in laravel 5.6

Hell folks,

In my project the .htaccess file is placed in the project root directory.

I want it placed in some other directory say '/public/setupfiles' and the project should be able to access it.

Is it possible? if yes thn how?



via Chebli Mohamed

dimanche 3 avril 2022

Laravel 5.6 ajax request internal server error

Hello i'm developing like disliked system based on https://www.itsolutionstuff.com/post/php-laravel-5-like-dislike-system-tutorialexample.html

but the ajax function throwing internal server error

this is my controller

public function ajaxRequest(Request $request)
    {
        $post = Post::find($request->id);
        $response = auth()->user()->toggleLiked($post);

        return response()->json(['success' => $response]);
    }

and this is my ajax request:

<script type="text/javascript">
    $(document).ready(function() {


        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });


        $('i.glyphicon-thumbs-up, i.glyphicon-thumbs-down').click(function(){
            var id = $(this).parents(".panel").data('id');
            var c = $('#'+this.id+'-bs3').html();
            var cObjId = this.id;
            var cObj = $(this);


            $.ajax({
               type:'POST',
               url:'/ajaxRequest',
               data:{id:id},
               success:function(data){
                  if(jQuery.isEmptyObject(data.success.attached)){
                    $('#'+cObjId+'-bs3').html(parseInt(c)-1);
                    $(cObj).removeClass("like-post");
                  }else{
                    $('#'+cObjId+'-bs3').html(parseInt(c)+1);
                    $(cObj).addClass("like-post");
                  }
               }
            });


        });


        $(document).delegate('*[data-toggle="lightbox"]', 'click', function(event) {
            event.preventDefault();
            $(this).ekkoLightbox();
        });
    });
</script>

this is the form for clicking the likes

    <span class="pull-right">
       <span class="like-btn">
       <i id="like" class="glyphicon glyphicon-thumbs-up "></i>
     
       <div id="like-bs3"></div>
     <span>
   </span>

what i have done is changing the routes in case it's missing something, but still throwing the same error, what should i do?



via Chebli Mohamed