0

I have this curl code

curl -X POST https://url.com -H 'authorization: Token YOUR_Session_TOKEN' -H 'content-type: application/json' -d '{"app_ids":["com.exmaple.app"], "data" : {"title":"Title", "content":"Content"}} 

that is used for push notification from web service to a mobile application. How can I use this code in PHP? I can't understand the -H and -d tags

3
  • 1
    If you have terminal you can then try the following command curl --help. You will find those flags. -H flag for setting header and -d flag for setting http post data. Commented Jan 12, 2020 at 7:53
  • @unclexo I don't know how to implement it in PHP Commented Jan 12, 2020 at 7:56
  • php.net/manual/en/curl.examples-basic.php Please refer this page for a basic cURL example in PHP Commented Jan 12, 2020 at 7:56

2 Answers 2

1

You can use this website to convert any of such: https://incarnate.github.io/curl-to-php/

But basically d is the payload (the data which you send with the request: usually POST or PUT); H stands for headers: each entry is another header.

So the most 1-to-1 example would be:

$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://url.com'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"app_ids\":[\"com.exmaple.app\"], \"data\" : {\"title\":\"Title\", \"content\":\"Content\"}}"); $headers = array(); $headers[] = 'Authorization: Token YOUR_Session_TOKEN'; $headers[] = 'Content-Type: application/json'; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($ch); if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); } curl_close($ch); 

but you can make it more dynamic and easy to manipulate based PHP variables by first creating an array with the attributes and then encoding it:

$ch = curl_init(); $data = [ 'app_ids' => [ 'com.example.app' ], 'data' => [ 'title' => 'Title', 'content' => 'Content' ] ]; curl_setopt($ch, CURLOPT_URL, 'https://url.com'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); $headers = array(); $headers[] = 'Authorization: Token YOUR_Session_TOKEN'; $headers[] = 'Content-Type: application/json'; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($ch); if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); } curl_close($ch); 

I suggest reading the manual of php-curl: https://www.php.net/manual/en/book.curl.php

Sign up to request clarification or add additional context in comments.

Comments

1

You may also do it in the following way:

<?php $url = "http://www.example.com"; $headers = [ 'Content-Type: application/json', 'Authorization: Token YOUR_Session_TOKEN' ]; $post_data = [ 'app_ids' => [ "com.exmaple.app" ], 'data' => [ 'title' => 'Title', 'content' => 'Content' ], ]; $post_data = json_encode($post_data); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); // For debugging $info = curl_getinfo($ch); curl_close($ch); echo '<pre>'; print_r($response); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.