XF 2.1 Trying to create a post from an external application via REST API is timing out

asprin

Active member
I'm in the process of testing out the API locally and as a first step I'm struggling to create a post in a thread. I've set up the thread:write scope on the API key from the ACP.

So, I put the following in a php file and upon running it, the request is timing out.
PHP:
function execute_curl($url, $data) {
    
    $curl = curl_init();
    $data_encoded = json_encode($data);
    curl_setopt($curl, CURLOPT_URL, $url);   
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        'XF-Api-Key: <MY API KEY>',
        "Content-Type: application/x-www-form-urlencoded",
        'Content-Length: ' . strlen($data_encoded))
    );
 
    
    $curl_response = curl_exec($curl);
    $result = json_decode($curl_response, true);
    curl_close($curl);
    unset($curl);
    
    return $result;
}
$data = array(
    'thread_id' => 7,
    'message' => 'This is sample post'
);
$response = execute_curl('http://localhost/xen/api/posts/', $data);
echo '<pre>';
print_r($response);
echo '</pre>';

(a) What am I doing wrong?
(b) I read that we shouldn't be using cURL when making requests from an add-on but given that this is an entirely different application, does the recommendation still hold true?


As you can see from the GIF below, I recorded the first 5 seconds of the transaction and it still keeps running and times out after about 3-4 minutes.

waiting.gif
 
Back
Top Bottom