2
\$\begingroup\$

I am building a Webhook model so users can receive data from a messaging app. I have never created such a system before, and I'd like to get some feedback on my attempt.

$url = 'https://www.example.com/user-webhook-request.php';
$handshakeKey = "1234abcd"; 

$data = array('title' => 'Hello',
'message' => 'World',
'url' => 'http://example.com/#check',
'image' => 'http://www.example.com/pics/file.jpg',
'handshakekey' => $handshakeKey);

$content = json_encode($data);

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);

$json_response = curl_exec($curl);
curl_close($curl);

Is there anything I should change to meet best or common practices?

\$\endgroup\$
3
\$\begingroup\$

I'm not familiar with that pattern, but I'll see what I can comment on.

  • $url is not a good name. What kind of URL is it? What importantance does the page have?
  • Is $handshakeKey an API key, or just some string that's checked on both ends? I'm assuming the latter, however, you should always check if the site you're working with has an official API. If it's all your project, I suggest you make an API.
  • What kind of $data is it? (Bad naming again)
  • Fix the indentation of $data. If you're using a version of PHP greater than 5.4, I suggest you use the square bracket notation. I think many find it easier to read.

    $data = [
        'title' => 'Hello',
        'message' => 'World',
        'url' => 'http://example.com/#check',
        'image' => 'http://www.example.com/pics/file.jpg',
        'handshakekey' => $handshakeKey
    ];
    
  • What kind of $content is it? Again, give these variables some meaning.

I don't see anything wrong with your cURL commands. Nice :)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.