1

I'm using https://github.com/jorenvh/laravel-share for a Laravel project. It works well except it doesn't work while adding multiple parameters to the URL which is going to be shared.

 public function ShareWidget(Request $request)
{

    $shareURL =  url('/') . '/newproducts' . '?' . http_build_query([
        'catalog' => $request->catalog,
        'category' => $request->category
    ]);


    $shareComponent = ShareFacade::page(
        $shareURL,
        null,
    )
        ->facebook()
        ->telegram()
        ->whatsapp();
    return view('share-component', compact('shareComponent'));
}

The ShareWidget function is responsible to make a URL and share it with the users on social medias. While I print out the ShareURL it shows what's supposed to be: http://127.0.0.1:8000/newproducts?catalog=1&category=11.

However, when its comes to the final stage which is the link that had been shared with users on social medias it misses the second parameter. No matter what it is. http://127.0.0.1:8000/newproducts?catalog=1.

As you see &category=11 part is missed in the second one.

Abdu4
  • 1,269
  • 2
  • 11
  • 19
  • you need to properly encode the ``$shareURL`` before passing to the ``page`` method. When you're sharing the url (e.g.) ``http://127.0.0.1:8000/newproducts?catalog=1&category=11`` and when rendered, it outputs as ``https://www.facebook.com/sharer/sharer.php?u=http://127.0.0.1:8000/newproducts?catalog=1&category=11`` & when user clicks on the link, it'll open FB and FB will only read ``http://127.0.0.1:8000/newproducts?catalog=1`` as the value of ``u`` parameter since the other param's are not encoded and will act as another parameter for the URL causing the issue. – OMi Shah Aug 20 '23 at 09:32

1 Answers1

0

You need to encode the URL before sending it to $shareComponent

$shareURL =  url('/') . '/newproducts' . '?' . http_build_query([
    'catalog' => $request->catalog,
    'category' => $request->category
]);

$shareURL = urlencode($shareURL);

for more details, read this...

Mustafa Poya
  • 2,615
  • 5
  • 22
  • 36
  • 1
    It solved the issue. Now it's working like a charm. I couldn't find to encode URL anywhere. I wonder why it happens? – Abdu4 Aug 20 '23 at 10:41
  • 1
    You are welcome, the problem is with & symbol which `http_build_query` escape this character which joins the parameters so you need to encode it before sending. – Mustafa Poya Aug 20 '23 at 10:43
  • 1
    This is a duplicate question and should have been marked duplicate instead of answering with duplicate answer. – OMi Shah Aug 20 '23 at 12:53