{"id":8433,"date":"2024-05-02T08:42:45","date_gmt":"2024-05-02T08:42:45","guid":{"rendered":"https:\/\/www.infinitivehost.com\/knowledge-base\/?p=8433"},"modified":"2024-05-02T10:12:53","modified_gmt":"2024-05-02T10:12:53","slug":"unlock-wordpress-rest-api-potential-with-php-curl","status":"publish","type":"post","link":"https:\/\/www.infinitivehost.com\/knowledge-base\/unlock-wordpress-rest-api-potential-with-php-curl\/","title":{"rendered":"Unlock WordPress REST API Potential with PHP cURL"},"content":{"rendered":"<div class='epvc-post-count'><span class='epvc-eye'><\/span>  <span class=\"epvc-count\"> 3,235<\/span><span class='epvc-label'> Views<\/span><\/div>\n<p class=\"wp-block-paragraph\">Sure, here&#8217;s an example of how you might use PHP cURL to make a request to the WordPress REST API, along with some error handling:<\/p>\n\n\n\n<pre class=\"wp-block-code has-vivid-red-color has-text-color has-link-color wp-elements-7791d43cc1feaa3efae6f34508ac502b\"><code><code>&lt;?php\n\n\/\/ Endpoint URL\n$endpoint = 'https:\/\/your-wordpress-site.com\/wp-json\/wp\/v2\/posts';\n\n\/\/ Data to send in the request\n$data = array(\n    'title' => 'Hello World',\n    'content' => 'This is a test post created via REST API.'\n);\n\n\/\/ Initialize cURL session\n$ch = curl_init();\n\n\/\/ Set cURL options\ncurl_setopt_array($ch, array(\n    CURLOPT_URL => $endpoint,\n    CURLOPT_RETURNTRANSFER => true,\n    CURLOPT_POST => true,\n    CURLOPT_POSTFIELDS => json_encode($data),\n    CURLOPT_HTTPHEADER => array(\n        'Content-Type: application\/json',\n        \/\/ Add any additional headers if needed, such as authentication headers\n    )\n));\n\n\/\/ Execute the request\n$response = curl_exec($ch);\n\n\/\/ Check for errors\nif ($response === false) {\n    \/\/ Handle cURL error\n    echo 'cURL error: ' . curl_error($ch);\n} else {\n    \/\/ Decode the response\n    $result = json_decode($response, true);\n\n    \/\/ Check if response contains error message\n    if(isset($result&#91;'code']) &amp;&amp; isset($result&#91;'message'])){\n        echo 'WordPress API Error: ' . $result&#91;'message'];\n    } else {\n        \/\/ Request was successful\n        echo 'Post created successfully. ID: ' . $result&#91;'id'];\n    }\n}\n\n\/\/ Close cURL session\ncurl_close($ch);\n?><\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Make sure to replace <code>'https:\/\/your-wordpress-site.com\/wp-json\/wp\/v2\/posts'<\/code> with the actual endpoint URL you want to access, and adjust the data being sent in the <code>$data<\/code> array according to your needs. Also, ensure that you handle any authentication required by your WordPress site, such as including authentication headers in the <code>CURLOPT_HTTPHEADER<\/code> array.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>3,235 Views Sure, here&#8217;s an example of how you might use PHP cURL to make a request to the WordPress REST API, along with some error handling: Make sure to replace &#8216;https:\/\/your-wordpress-site.com\/wp-json\/wp\/v2\/posts&#8217; with the actual endpoint URL you want to access, and adjust the data being sent in the $data array according to your needs. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[76],"tags":[],"class_list":["post-8433","post","type-post","status-publish","format-standard","hentry","category-wordpress"],"_links":{"self":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/8433","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/comments?post=8433"}],"version-history":[{"count":1,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/8433\/revisions"}],"predecessor-version":[{"id":8434,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/8433\/revisions\/8434"}],"wp:attachment":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/media?parent=8433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/categories?post=8433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/tags?post=8433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}