0

I am hoping someone can help with this. I am getting content via an API call to a third party vendor that supplies articles. I need to then add these articles as posts and also make sure I don't duplicate. My API response is json_decoded.

here is my code so far:

function call_api($url, $data) {

    $curl = curl_init();

    switch ($method){
        case "POST":
            curl_setopt($curl, CURLOPT_POST, 1);
            if ($data)
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        break;
        case "PUT":
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
            if ($data)
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);                              
        break;
        default:
            if ($data)
                $url = sprintf("%s?%s", $url, http_build_query($data));
    }

    // OPTIONS:
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Authorization: XXXXXXXXXXXXXX',
    ));
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

    // EXECUTE:
    $result = curl_exec($curl);

    if(!$result) {
        die("Connection Failure");
    }

    curl_close($curl);

    return $result;

}

$result = call_api('XXXXXX', array("by_community" => "XXXX", 'status' => 'published'));

$response = json_decode($result, true);
  • Is this the code you are using to get the articles from a third party vendor? Then it is not really relevant to your question. Please show us the code you are using to insert new posts into the WP. – dboris Oct 18 '18 at 13:03
  • I do not have the code to insert the posts yet. That is what I am asking for help on :) – Jason Oct 18 '18 at 13:04
  • I think you would get better help if you first read some basic WP Rest API Documentation, and maybe some other tutorials, and then try to do it yourself, and ask a question with concrete problem you faced. – dboris Oct 18 '18 at 13:08

Your Answer

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

Browse other questions tagged or ask your own question.