0

I'm working on a wordpress project where I have to modify my theme, so I can request a JSON to an external API. I've been searching through the internet how to do that and a lot of people use CURL.

I must do a POST request, yet I don't know how it works or how to do it. So far I've got this code running:

 $url='api.example.com/v1/property/search/';

 $data_array =  array(

            $id_company     =>  '123456',
            $api_token     =>  'abcd_efgh_ijkl_mnop',
    );

        $curl = curl_init();

        curl_setopt($curl, CURLOPT_POST, 1);

        curl_setopt($curl, CURLOPT_POSTFIELDS, $data_array);
        curl_setopt($curl, CURLOPT_URL, $url);
         curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        'APIKEY: 111111111111111111111',
        'Content-Type: application/json'
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

        $result = curl_exec($curl);
        if(!$result){die("Connection Failure");}
        curl_close($curl);
         echo($result);

I don't know where exactly I should put my authentication info or how does the curl methods work in PHP. Can you guys check it out and help me solve this?

Shirux
  • 143
  • 5
  • curl is a relic of php and a pain to use. I suggest using http://docs.guzzlephp.org which comes as a composer library (if you have any experience with that). However the question itself doesnt really make sense to me, as in how is a wordpress theme related to doing a POST request in php? – Flame Mar 13 '19 at 21:17
  • Is this your actual code there? you never close the array after this line `curl_setopt($curl, CURLOPT_HTTPHEADER, array(` – ArSeN Mar 13 '19 at 21:17

1 Answers1

1

There are some answers out there that would help you, such as this one.

However, WordPress actually has built-in functions to make GET and POST requests (that actually fall back to cURL I believe?) named wp_remote_get() and wp_remote_post(). Clearly in your case, you'll want to make use of wp_remote_post().

$url = 'https://api.example.com/v1/property/search/';

$data_array = array(
    'id_company' => 123456,
    'api_token'  => 'abcde_fgh'
);

$headers = array(
    'APIKEY' => 1111111111,
    'Content-Type' => 'application/json'
);

$response = wp_remote_post( $url, array(
        'method' => 'POST',
        'timeout' => 45,
        'redirection' => 5,
        'httpversion' => '1.0',
        'blocking' => true,
        'headers' => $headers,
        'body' => $data_array,
        'cookies' => array()
    )
);

if( is_wp_error( $response ) ){
    $error_message = $response->get_error_message();
    echo "Something went wrong: $error_message";
} else {
    echo 'Success! Response:<pre>';
        print_r( $response );
    echo '</pre>';
}
Xhynk
  • 13,513
  • 8
  • 32
  • 69
  • Thanks for the answer. I just replaced the code I have with the one you just gave me. I got a {"status":"error"} from the server. I wish to know where do i pass the id and token the API gives me? and how Do I must structure that authentication info? – Shirux Mar 13 '19 at 22:00
  • They application you're sending data to will have the specifics of how they want the data. Typically it's as a `key => value` pair in the body or headers, but it may very will be different based on the service you're sending the data to – Xhynk Mar 13 '19 at 23:06
  • Thanks for both answers, really helped me a lot =) – Shirux Mar 14 '19 at 22:30