0

I want to make a query with curl but I do not know which url to give it

I have tried this:

// Préparation des données
$data = [];
$data["code__client"] = "VERTDIS13";
$data["status"] = "90";
$data["numero__incident"] = "INC544842";

// On tranforme le tableau PHP en objet JSON
$data = json_encode($data);

// Envoi au serveur
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"/notification/server");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "data=" . $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close($ch);

// Ici pour tester j'affiche ce que le serveur renvoi
echo $server_output;

but the page only reload and nothing happens

I put

dd($server_output);

and this return false

what 's the issue ?

Thanks for you help

  • 1
    You need to define a route to point to your controller method rather than referring to a filesystem path as you have here. Start with https://laravel.com/docs/master/routing – iainn May 03 '18 at 14:13
  • I try this : curl_setopt($ch, CURLOPT_URL,"/notification/server"); but now the page reload and nothing happend – Gautier Drincqbier May 03 '18 at 14:23
  • @GautierDrincqbier you should show your research in the body of the question. Just [edit] your question and put these details in it -- comments can be deleted, and SO is not a threaded forum. –  May 03 '18 at 14:27
  • ok sorry I edit it ! thanks ! – Gautier Drincqbier May 03 '18 at 14:36
  • How should we know which route you want to use? Can you explain this? – Nico Haase May 09 '18 at 13:53
  • I have change the url (now it's http://localhost:3000/notification/server) and it's work i can print message (with dd()) . the code is launch but at the end a always have the message sessions expired – Gautier Drincqbier May 09 '18 at 14:02

1 Answers1

0

When curl_exec() returns bool false the curl request failed, in that case you can retrieve a curl error message with:

$error = curl_error($ch);
dd($error);
  • thanks for the answer, when I did it I have the message `code` " malformed" `code` so I find it's the url format and I change it but I had " the section has expired " so i change the controller and just put the code directly and I have a error but I think it's my code so thanks you for your help ;p – Gautier Drincqbier May 04 '18 at 07:56
  • OK, the malformed url error message is probably because curl doesn't work with relative urls. You could try to use the full url of the page you want to load (https://www.example.com/notification/server). And maybe you should also question why your loading something from the same server via curl. Maybe there's a more reasonable option to achieve what you need to do? – Christian 'Otsch' Olear May 04 '18 at 13:49
  • yes I change the url for the full url and this work but now the problem is when he suppose to launch the function I have session expired ... i load something from the same server but after that can change thanks for you help. – Gautier Drincqbier May 04 '18 at 14:19
  • I saw that you've added a new question for your [session problem](https://stackoverflow.com/questions/50175965/curl-with-laravel-return-session-expired), so I flagged this post to be closed. I hope that's ok. – Christian 'Otsch' Olear May 04 '18 at 15:27