0

I am trying to test something in my website . How i can create the HTTP requests in PHP using code cURL,

This my code

public function test(){
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'exmaple.com:80/',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_HTTPHEADER => array('Content-Type:application/json','Authorization: Basic'),
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_POST => 1,
            CURLOPT_POSTFIELDS => "\n  {\n
                                            \"M\": \"\",\n
                                            \"A\": \"\"\n                          
                                        }",
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
        ));
        echo $response = curl_exec($curl);
        die();
    }

i have add this code in index.php file and i open link example.com/index.php but its shows error white page nothing happened !

<?php
 $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => 'exmaple.com:80/',
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_HTTPHEADER => array('Content-Type:application/json','Authorization: Basic'),
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_POST => 1,
                CURLOPT_POSTFIELDS => "\n  {\n
                                                \"M\": \"\",\n
                                                \"A\": \"\"\n                          
                                            }",
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'POST',
            ));
            echo $response = curl_exec($curl);
            die();
        }
?> 

this error i am getting when i open the index.php

Notice: Undefined variable: curl in /var/www/html/index.php on line 5

Warning: curl_setopt_array() expects parameter 1 to be resource, null given in /var/www/html/index.php on line 6

Notice: Undefined variable: curl in /var/www/html/index.php on line 21

Warning: curl_exec() expects parameter 1 to be resource, null given in /var/www/html/index.php on line 21

How i can fix this?

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Forten
  • 1
  • 3
  • It shows an error or a white page? Is error reporting enabled? – user3783243 Sep 29 '22 at 14:46
  • 1
    Nothing is being closed after `die();` so the `}` should be removed. – user3783243 Sep 29 '22 at 14:46
  • 1
    Read this: [How can I get useful error messages in PHP?](https://stackoverflow.com/questions/845021/how-can-i-get-useful-error-messages-in-php) – ADyson Sep 29 '22 at 14:47
  • 1
    _Side note:_ Instead of manually writing the JSON, create a PHP array in the format you want and run it through [json_encode()](https://www.php.net/manual/en/function.json-encode.php), something like: `CURLOPT_POSTFIELDS => json_encode(['M' => '', 'A' => '']),`. Then you can at least be sure that the JSON you're going to send is valid. – M. Eriksson Sep 29 '22 at 15:10
  • @user3783243 Nothing shows its replys 200 OK – Forten Sep 29 '22 at 17:01
  • @M.Eriksson Can you explain more , ? How it could done by this code? – Forten Sep 29 '22 at 17:02
  • Just replace your json string with the `json_encode(...)` function I wrote above. I also posted a link to the manual about that function so you can read about it. Not sure how I can make it much clearer, tbh. – M. Eriksson Sep 29 '22 at 18:51

0 Answers0