{"id":1143,"date":"2018-12-04T23:00:00","date_gmt":"2018-12-04T23:00:00","guid":{"rendered":"https:\/\/old-2023.weichie.com\/php-curl-api-calls-authentication\/"},"modified":"2024-02-08T15:13:03","modified_gmt":"2024-02-08T15:13:03","slug":"php-curl-api-calls-authentication","status":"publish","type":"post","link":"https:\/\/old-2023.weichie.com\/nl\/blog\/php-curl-api-calls-authentication\/","title":{"rendered":"PHP cURL API calls with authentication (REST GET POST)"},"content":{"rendered":"<div class=\"default__block container-fluid lg\">\n<p class=\"wp-block-paragraph\">If you&#8217;re here because you want to connect your php code to an external API, please check my <a href=\"https:\/\/old-2023.weichie.com\/nl\/blog\/curl-api-calls-with-php\/\">cURL api-calls with php<\/a>\u00a0tutorial first. This is part 2 of how to connect to an API using cURL in php, as I received a lot of questions on how to connect if the API requires authentication (utoken) first.<\/p>\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<!--more-->\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<p class=\"wp-block-paragraph\"><strong>Heads up<\/strong>: This time, I will be using Javascript for more flexibility. As I need to make an &#8216;authentication&#8217; api call first before our initial GET call. Therefore I create my API calls in my own php files on the side, and will target these files using ES6 async\/fetch from within my page. If you see me linking to my-site.com, this means I created the file myself, on my own server. If I&#8217;m linking to api-site.com, this means I&#8217;m making a call the the external API.<\/p>\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/old-2023.weichie.com\/wp-content\/uploads\/2023\/02\/api_2.jpg\" alt=\"cURL Authentication\" class=\"wp-image-734\"\/><\/figure>\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<h2 class=\"wp-block-heading\" id=\"h-generating-an-auth-key\">Generating an auth key<\/h2>\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<p class=\"wp-block-paragraph\">Some API&#8217;s only allow POST or GET requests if you use an <strong>auth-token<\/strong>. We need to generate this auth-token first, before we are allowed to make API calls. Normally the API docs should explain how you can generate their auth-token. In my example, I can generate an auth-token by posting my API client ID, client_secret and a login type to their API Auth file.<\/p>\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<p class=\"wp-block-paragraph\">Here is how I can generate and use my auth-token, based on the cURL script of my <a href=\"https:\/\/old-2023.weichie.com\/nl\/blog\/curl-api-calls-with-php\/\">part-1 tutorial<\/a>. In this tutorial, I&#8217;ll be calling this file <strong>api\/auth.php<\/strong><\/p>\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<pre><code class=\"language-php\">$curl = curl_init();\n$auth_data = array(\n\t'client_id' \t\t=&gt; 'XBnKaywRCrj05mM-XXX-6DXuZ3FFkUgiw45',\n\t'client_secret' \t=&gt; 'btHTWVNMUATHEnF-XXX-2nQabKcKVo3VXtU',\n\t'grant_type' \t\t=&gt; 'client_credentials'\n);\ncurl_setopt($curl, CURLOPT_POST, 1);\ncurl_setopt($curl, CURLOPT_POSTFIELDS, $auth_data);\ncurl_setopt($curl, CURLOPT_URL, 'https:\/\/api-site.com\/oauth\/token');\ncurl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);\ncurl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);\n$result = curl_exec($curl);\nif(!$result){die(\"Connection Failure\");}\ncurl_close($curl);\necho $result;<\/code><\/pre>\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<p class=\"wp-block-paragraph\">This will return an auth-token for me. With this auth token, I can start making normal API calls like POST and GET. However.. My example API only keeps my auth-token alive for 2 weeks AND will only allow 3 auth-tokens at a time. So for each call I make, I need to regenerate this. This is why I&#8217;m using JS as example, so we can easily quickly generate the AUTH token before our own call. For this, I have a separate JS function to post to our own api\/auth.php: <strong>JS<\/strong><\/p>\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<pre><code class=\"language-php\">const apiAuth = async () =&gt; {\n  try{\n    const response  = await fetch('https:\/\/my-site.com\/api\/auth.php');\n    if(response.ok){\n      return response.json();\n    }\n    throw new Error(`apiAuth_response: ${response}`);\n  }catch(error){\n    console.error(`apiAuth: ${error}`);\n  }\n}<\/code><\/pre>\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<p class=\"wp-block-paragraph\">This is the JS-script and the PHP-script I&#8217;m using to generate an auth-token for my API. I&#8217;m using await to make sure our script only continuous when we do have received the Auth token. If we don&#8217;t await this token, it&#8217;s possible the rest of our script will already try to make the API call without the utoken, and will return errors.<\/p>\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<h2 class=\"wp-block-heading\" id=\"h-how-to-use-our-auth-token\">How to use our Auth-token?<\/h2>\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<p class=\"wp-block-paragraph\">Ok good, now we have an auth-token for our app&#8230; How do we use this?<\/p>\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<h3 class=\"wp-block-heading\" id=\"h-curl-get-request-with-authentication\">cURL GET request (with Authentication)<\/h3>\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<p class=\"wp-block-paragraph\">In most cases (I think) you need to add your auth-token to the url you&#8217;re using to make a valid API call. Again, you should be able to find this in the documentation of the API your using. In my example, if I want to make an API call, my link should look like this: <strong>api\/get_all_reviews.php<\/strong><\/p>\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<pre><code class=\"language-php\">https:\/\/api-site.com\/v1\/apps\/' . $app_key . '\/reviews?utoken=' . $utoken;<\/code><\/pre>\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<p class=\"wp-block-paragraph\">We do know our $app_key and we just generated our $utoken. So our .php file to make the GET-call would look like this: ($_POST[&#8216;auth_token&#8217;] will be our received utoken from our previous function.<\/p>\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<pre><code class=\"language-php\">if(isset($_POST['access_token'])){\t\n\t$app_key  = 'XBnKaywRCrj05m-XXX-v6DXuZ3FFkUgiw45';\n\t$utoken = $_POST['access_token'];\n\t$url = 'https:\/\/api-site.com\/v1\/apps\/' . $app_key . '\/reviews?utoken=' . $utoken;\n\t\n\t$curl = curl_init();\n   curl_setopt($curl, CURLOPT_URL, $url);\n   curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);\n   curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);\n   $result = curl_exec($curl);\n   if(!$result){die(\"Connection Failure\");}\n   curl_close($curl);\n   echo $result;\n}<\/code><\/pre>\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<p class=\"wp-block-paragraph\">This will return all our product-reviews in the example I picked for this tutorial. Now we still need to target this GET php file with our JS and don&#8217;t forget to generate our utoken first! <strong>JS<\/strong><\/p>\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<pre><code class=\"language-javascript\">const getAllReviews = async () =&gt; {\n  const auth = await apiAuth();\n  const data = {\n    access_token: auth.access_token,\n    token_type: auth.token_type,\n  }\n  try{\n    $.post({\n      type: 'POST',\n      url: 'http:\/\/my-site.com\/api\/get_all_reviews.php',\n      data: data\n    })\n    .success(res =&gt; {\n      const data = JSON.parse(res);\n      const reviews = data.reviews;\n      displayAllReviews(reviews, $('.review-list'));\n    });\n  }catch(error){\n    console.error(`getAllReviews: ${error}`);\n  }\n}<\/code><\/pre>\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<p class=\"wp-block-paragraph\">As you can see we await the Auth function again, before we continue with our original API call. We post the received auth.access_token together with our form data to our GET php script. The displayAllReviews() is a random function, that I am passing our received data to. So here you can call your own functions, depending on what you want to do with this data.<\/p>\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<h3 class=\"wp-block-heading\" id=\"h-curl-post-request-with-authentication\">cURL POST request (with Authentication)<\/h3>\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<p class=\"wp-block-paragraph\">Same for POST as with GET, we need to add our utoken (Auth-token) to the url. Here is an example of my POST link to the API, with my utoken:&nbsp;<strong>api\/post_review.php<\/strong><\/p>\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<pre><code class=\"language-php\">if(isset($_POST['success'])){\t\n\t$p_url = 'https:\/\/product-link.com';\n\t$email = $_POST['email'];\n\t\n\t$post_array = array(\n\t\t'appkey' =&gt; 'XBnKaywRCrj05m-XXX-v6DXuZ3FFkUgiw45',\n\t\t'domain' =&gt; 'https:\/\/api-site.com',\n\t\t'product_url' =&gt; $p_url,\n\t\t'email' =&gt; $email,\n\t\t'review_content' =&gt; $_POST['message'],\n\t\t'review_title' =&gt; $_POST['title'],\n\t\t'review_score' =&gt; $_POST['star_rating_value']\n\t);\n\tpostReview($post_array);\n}else{\n\t$response = array(\n\t\t'response' \t=&gt; 'error',\n\t\t'message'\t=&gt; 'POST is required to use this function'\n\t);\n}\nfunction postReview($post_array){\n\t$curl = curl_init();\n\tcurl_setopt($curl, CURLOPT_POST, 1);\n\tcurl_setopt($curl, CURLOPT_POSTFIELDS, $post_array);\n\tcurl_setopt($curl, CURLOPT_URL, 'https:\/\/api-site.com\/v1\/reviews');\n\tcurl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);\n\tcurl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);\n\t\n\t$result = curl_exec($curl);\n\tif(!$result){die(\"Connection Failure\");}\n\tcurl_close($curl);\n\techo $result;\n}<\/code><\/pre>\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<p class=\"wp-block-paragraph\">To make a POST, I first gather all the information I need to make the post. (like user-email and the product URL I want to post a review for). When I have all the data, I call the postReview function, that will make the POST API call. We need to gather all this data up-front, otherwise we are not allowed to make our API call. As our POST request doesn&#8217;t require a utoken variable.<\/p>\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<p class=\"wp-block-paragraph\">Now the JS script I used for this: <strong>JS<\/strong><\/p>\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<pre><code class=\"language-javascript\">function postSiteReview(data, form){\n  $.ajax({\n    url: 'https:\/\/my-site.com\/api\/post_review.php',\n    type: 'post',\n    data: data,\n    success: function(result){\n      let res = JSON.parse(result);\n      let message = '';\n      if(res.code === 200){\n        form.find('.success-message').fadeIn();\n      }else{\n        if(res.response === 'error'){\n          message = res.message;\n        }\n        if(res.status &amp;&amp; res.status.code === 500){\n          message = res.status.message;\n        }\n        $('p.error-msg').text(`Uh ooh.. ${message}`);\n      }\n    },\n    error: function(err){\n      console.log('$.ajax error: ' + err);\n    }\n  });\n}<\/code><\/pre>\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<h2 class=\"wp-block-heading\" id=\"h-no-access-control-allow-origin-header-is-present-cors\">No Access-Control-Allow-Origin header is present &#8211; (CORS)<\/h2>\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<p class=\"wp-block-paragraph\">If you&#8217;re making JS calls to your own php-files, but your files are on a different server, please at following line at the top of each .php file you&#8217;re using:<\/p>\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<pre><code class=\"language-php\">header(\"Access-Control-Allow-Origin: https:\/\/my-site.com\");<\/code><\/pre>\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<p class=\"wp-block-paragraph\">This should fix the access-control error you&#8217;re seeing in the js-console.<\/p>\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<p class=\"wp-block-paragraph\">I really hope this part is well-explained as well. If not, please feel free to ask in the comments. (I can not guarantee you will get a response).<\/p>\n<\/div>\n\n<div class=\"default__block container-fluid lg\">\n<p class=\"wp-block-paragraph\">Let me know if it works for you.<br>\nHappy coding!<\/p>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re here because you want to connect your php code to an external API, please check my cURL api-calls with php\u00a0tutorial first. This is part 2 of how to connect to an API using cURL in php, as I received a lot of questions on how to connect if the API requires authentication (utoken) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":711,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[211],"tags":[137,268,260,274,162,275,186,198],"class_list":["post-1143","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development-nl","tag-api-nl","tag-authentication-nl","tag-es6-nl","tag-get-nl","tag-javascript-nl","tag-json-nl","tag-php-nl","tag-post-nl"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.9 (Yoast SEO v27.9) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>PHP cURL API calls with authentication (REST GET POST) - Weichie.com<\/title>\n<meta name=\"description\" content=\"My previous cURL API call with PHP post got so much traffic, I received a lot of questions about how you could add authentication to the cURL API calls.\" \/>\n<meta name=\"robots\" content=\"noindex, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"nl_NL\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP cURL API calls with authentication (REST GET POST) - Weichie.com\" \/>\n<meta property=\"og:description\" content=\"My previous cURL API call with PHP post got so much traffic, I received a lot of questions about how you could add authentication to the cURL API calls.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/old-2023.weichie.com\/nl\/blog\/php-curl-api-calls-authentication\/\" \/>\n<meta property=\"og:site_name\" content=\"Weichie.com\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/weichiecom\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-12-04T23:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-08T15:13:03+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/old-2023.weichie.com\/wp-content\/uploads\/2023\/02\/api_2_1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1080\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"weichie\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"cURL API calls with authentication in php (GET POST) - Bob Weichler\" \/>\n<meta name=\"twitter:description\" content=\"My previous cURL API call with PHP post got so much traffic, I received a lot of questions about how you could add authentication to the cURL API calls.\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/www.weichieprojects.com\/wp-content\/uploads\/2018\/12\/api_2_1.jpg\" \/>\n<meta name=\"twitter:label1\" content=\"Geschreven door\" \/>\n\t<meta name=\"twitter:data1\" content=\"weichie\" \/>\n\t<meta name=\"twitter:label2\" content=\"Geschatte leestijd\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minuten\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\\\/\\\/old-2023.weichie.com\\\/nl\\\/blog\\\/php-curl-api-calls-authentication\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/old-2023.weichie.com\\\/nl\\\/blog\\\/php-curl-api-calls-authentication\\\/\"},\"author\":{\"name\":\"weichie\",\"@id\":\"https:\\\/\\\/old-2023.weichie.com\\\/nl\\\/#\\\/schema\\\/person\\\/36b4fff07382bc08a5c566728d9c579f\"},\"headline\":\"PHP cURL API calls with authentication (REST GET POST)\",\"datePublished\":\"2018-12-04T23:00:00+00:00\",\"dateModified\":\"2024-02-08T15:13:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/old-2023.weichie.com\\\/nl\\\/blog\\\/php-curl-api-calls-authentication\\\/\"},\"wordCount\":854,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/old-2023.weichie.com\\\/nl\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/old-2023.weichie.com\\\/nl\\\/blog\\\/php-curl-api-calls-authentication\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/old-2023.weichie.com\\\/wp-content\\\/uploads\\\/2023\\\/02\\\/api_2_1.jpg\",\"keywords\":[\"api\",\"authentication\",\"ES6\",\"get\",\"javascript\",\"json\",\"php\",\"post\"],\"articleSection\":[\"Development\"],\"inLanguage\":\"nl-NL\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/old-2023.weichie.com\\\/nl\\\/blog\\\/php-curl-api-calls-authentication\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/old-2023.weichie.com\\\/nl\\\/blog\\\/php-curl-api-calls-authentication\\\/\",\"url\":\"https:\\\/\\\/old-2023.weichie.com\\\/nl\\\/blog\\\/php-curl-api-calls-authentication\\\/\",\"name\":\"PHP cURL API calls with authentication (REST GET POST) - Weichie.com\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/old-2023.weichie.com\\\/nl\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/old-2023.weichie.com\\\/nl\\\/blog\\\/php-curl-api-calls-authentication\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/old-2023.weichie.com\\\/nl\\\/blog\\\/php-curl-api-calls-authentication\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/old-2023.weichie.com\\\/wp-content\\\/uploads\\\/2023\\\/02\\\/api_2_1.jpg\",\"datePublished\":\"2018-12-04T23:00:00+00:00\",\"dateModified\":\"2024-02-08T15:13:03+00:00\",\"description\":\"My previous cURL API call with PHP post got so much traffic, I received a lot of questions about how you could add authentication to the cURL API calls.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/old-2023.weichie.com\\\/nl\\\/blog\\\/php-curl-api-calls-authentication\\\/#breadcrumb\"},\"inLanguage\":\"nl-NL\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/old-2023.weichie.com\\\/nl\\\/blog\\\/php-curl-api-calls-authentication\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"nl-NL\",\"@id\":\"https:\\\/\\\/old-2023.weichie.com\\\/nl\\\/blog\\\/php-curl-api-calls-authentication\\\/#primaryimage\",\"url\":\"https:\\\/\\\/old-2023.weichie.com\\\/wp-content\\\/uploads\\\/2023\\\/02\\\/api_2_1.jpg\",\"contentUrl\":\"https:\\\/\\\/old-2023.weichie.com\\\/wp-content\\\/uploads\\\/2023\\\/02\\\/api_2_1.jpg\",\"width\":1080,\"height\":1080},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/old-2023.weichie.com\\\/nl\\\/blog\\\/php-curl-api-calls-authentication\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/old-2023.weichie.com\\\/nl\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP cURL API calls with authentication (REST GET POST)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/old-2023.weichie.com\\\/nl\\\/#website\",\"url\":\"https:\\\/\\\/old-2023.weichie.com\\\/nl\\\/\",\"name\":\"Weichie.com\",\"description\":\"Digital Agency in Brussels &amp; New York\",\"publisher\":{\"@id\":\"https:\\\/\\\/old-2023.weichie.com\\\/nl\\\/#organization\"},\"alternateName\":\"Weichie\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/old-2023.weichie.com\\\/nl\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"nl-NL\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/old-2023.weichie.com\\\/nl\\\/#organization\",\"name\":\"Weichie.com\",\"alternateName\":\"Weichie\",\"url\":\"https:\\\/\\\/old-2023.weichie.com\\\/nl\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"nl-NL\",\"@id\":\"https:\\\/\\\/old-2023.weichie.com\\\/nl\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/old-2023.weichie.com\\\/wp-content\\\/uploads\\\/2023\\\/11\\\/Weichie_full.png\",\"contentUrl\":\"https:\\\/\\\/old-2023.weichie.com\\\/wp-content\\\/uploads\\\/2023\\\/11\\\/Weichie_full.png\",\"width\":1181,\"height\":177,\"caption\":\"Weichie.com\"},\"image\":{\"@id\":\"https:\\\/\\\/old-2023.weichie.com\\\/nl\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/weichiecom\\\/\",\"https:\\\/\\\/www.instagram.com\\\/weichiecom\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/weichie\\\/\"],\"description\":\"A digital agency based in Brussels and New York. We create high-quality digital experiences for brands and help them promote their business through the online world! Specialised in websites, e-commerce, SEO and applications.\",\"email\":\"hello@weichie.com\",\"telephone\":\"+32 469 129 449\",\"legalName\":\"Weichie.com\",\"vatID\":\"0782.257.983\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"1\",\"maxValue\":\"10\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/old-2023.weichie.com\\\/nl\\\/#\\\/schema\\\/person\\\/36b4fff07382bc08a5c566728d9c579f\",\"name\":\"weichie\",\"sameAs\":[\"https:\\\/\\\/old-2023.weichie.com\"]}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"PHP cURL API calls with authentication (REST GET POST) - Weichie.com","description":"My previous cURL API call with PHP post got so much traffic, I received a lot of questions about how you could add authentication to the cURL API calls.","robots":{"index":"noindex","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"og_locale":"nl_NL","og_type":"article","og_title":"PHP cURL API calls with authentication (REST GET POST) - Weichie.com","og_description":"My previous cURL API call with PHP post got so much traffic, I received a lot of questions about how you could add authentication to the cURL API calls.","og_url":"https:\/\/old-2023.weichie.com\/nl\/blog\/php-curl-api-calls-authentication\/","og_site_name":"Weichie.com","article_publisher":"https:\/\/www.facebook.com\/weichiecom\/","article_published_time":"2018-12-04T23:00:00+00:00","article_modified_time":"2024-02-08T15:13:03+00:00","og_image":[{"width":1080,"height":1080,"url":"https:\/\/old-2023.weichie.com\/wp-content\/uploads\/2023\/02\/api_2_1.jpg","type":"image\/jpeg"}],"author":"weichie","twitter_card":"summary_large_image","twitter_title":"cURL API calls with authentication in php (GET POST) - Bob Weichler","twitter_description":"My previous cURL API call with PHP post got so much traffic, I received a lot of questions about how you could add authentication to the cURL API calls.","twitter_image":"https:\/\/www.weichieprojects.com\/wp-content\/uploads\/2018\/12\/api_2_1.jpg","twitter_misc":{"Geschreven door":"weichie","Geschatte leestijd":"4 minuten"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/old-2023.weichie.com\/nl\/blog\/php-curl-api-calls-authentication\/#article","isPartOf":{"@id":"https:\/\/old-2023.weichie.com\/nl\/blog\/php-curl-api-calls-authentication\/"},"author":{"name":"weichie","@id":"https:\/\/old-2023.weichie.com\/nl\/#\/schema\/person\/36b4fff07382bc08a5c566728d9c579f"},"headline":"PHP cURL API calls with authentication (REST GET POST)","datePublished":"2018-12-04T23:00:00+00:00","dateModified":"2024-02-08T15:13:03+00:00","mainEntityOfPage":{"@id":"https:\/\/old-2023.weichie.com\/nl\/blog\/php-curl-api-calls-authentication\/"},"wordCount":854,"commentCount":0,"publisher":{"@id":"https:\/\/old-2023.weichie.com\/nl\/#organization"},"image":{"@id":"https:\/\/old-2023.weichie.com\/nl\/blog\/php-curl-api-calls-authentication\/#primaryimage"},"thumbnailUrl":"https:\/\/old-2023.weichie.com\/wp-content\/uploads\/2023\/02\/api_2_1.jpg","keywords":["api","authentication","ES6","get","javascript","json","php","post"],"articleSection":["Development"],"inLanguage":"nl-NL","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/old-2023.weichie.com\/nl\/blog\/php-curl-api-calls-authentication\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/old-2023.weichie.com\/nl\/blog\/php-curl-api-calls-authentication\/","url":"https:\/\/old-2023.weichie.com\/nl\/blog\/php-curl-api-calls-authentication\/","name":"PHP cURL API calls with authentication (REST GET POST) - Weichie.com","isPartOf":{"@id":"https:\/\/old-2023.weichie.com\/nl\/#website"},"primaryImageOfPage":{"@id":"https:\/\/old-2023.weichie.com\/nl\/blog\/php-curl-api-calls-authentication\/#primaryimage"},"image":{"@id":"https:\/\/old-2023.weichie.com\/nl\/blog\/php-curl-api-calls-authentication\/#primaryimage"},"thumbnailUrl":"https:\/\/old-2023.weichie.com\/wp-content\/uploads\/2023\/02\/api_2_1.jpg","datePublished":"2018-12-04T23:00:00+00:00","dateModified":"2024-02-08T15:13:03+00:00","description":"My previous cURL API call with PHP post got so much traffic, I received a lot of questions about how you could add authentication to the cURL API calls.","breadcrumb":{"@id":"https:\/\/old-2023.weichie.com\/nl\/blog\/php-curl-api-calls-authentication\/#breadcrumb"},"inLanguage":"nl-NL","potentialAction":[{"@type":"ReadAction","target":["https:\/\/old-2023.weichie.com\/nl\/blog\/php-curl-api-calls-authentication\/"]}]},{"@type":"ImageObject","inLanguage":"nl-NL","@id":"https:\/\/old-2023.weichie.com\/nl\/blog\/php-curl-api-calls-authentication\/#primaryimage","url":"https:\/\/old-2023.weichie.com\/wp-content\/uploads\/2023\/02\/api_2_1.jpg","contentUrl":"https:\/\/old-2023.weichie.com\/wp-content\/uploads\/2023\/02\/api_2_1.jpg","width":1080,"height":1080},{"@type":"BreadcrumbList","@id":"https:\/\/old-2023.weichie.com\/nl\/blog\/php-curl-api-calls-authentication\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/old-2023.weichie.com\/nl\/"},{"@type":"ListItem","position":2,"name":"PHP cURL API calls with authentication (REST GET POST)"}]},{"@type":"WebSite","@id":"https:\/\/old-2023.weichie.com\/nl\/#website","url":"https:\/\/old-2023.weichie.com\/nl\/","name":"Weichie.com","description":"Digital Agency in Brussels &amp; New York","publisher":{"@id":"https:\/\/old-2023.weichie.com\/nl\/#organization"},"alternateName":"Weichie","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/old-2023.weichie.com\/nl\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"nl-NL"},{"@type":"Organization","@id":"https:\/\/old-2023.weichie.com\/nl\/#organization","name":"Weichie.com","alternateName":"Weichie","url":"https:\/\/old-2023.weichie.com\/nl\/","logo":{"@type":"ImageObject","inLanguage":"nl-NL","@id":"https:\/\/old-2023.weichie.com\/nl\/#\/schema\/logo\/image\/","url":"https:\/\/old-2023.weichie.com\/wp-content\/uploads\/2023\/11\/Weichie_full.png","contentUrl":"https:\/\/old-2023.weichie.com\/wp-content\/uploads\/2023\/11\/Weichie_full.png","width":1181,"height":177,"caption":"Weichie.com"},"image":{"@id":"https:\/\/old-2023.weichie.com\/nl\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/weichiecom\/","https:\/\/www.instagram.com\/weichiecom\/","https:\/\/www.linkedin.com\/company\/weichie\/"],"description":"A digital agency based in Brussels and New York. We create high-quality digital experiences for brands and help them promote their business through the online world! Specialised in websites, e-commerce, SEO and applications.","email":"hello@weichie.com","telephone":"+32 469 129 449","legalName":"Weichie.com","vatID":"0782.257.983","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"1","maxValue":"10"}},{"@type":"Person","@id":"https:\/\/old-2023.weichie.com\/nl\/#\/schema\/person\/36b4fff07382bc08a5c566728d9c579f","name":"weichie","sameAs":["https:\/\/old-2023.weichie.com"]}]}},"_links":{"self":[{"href":"https:\/\/old-2023.weichie.com\/nl\/wp-json\/wp\/v2\/posts\/1143","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/old-2023.weichie.com\/nl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/old-2023.weichie.com\/nl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/old-2023.weichie.com\/nl\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/old-2023.weichie.com\/nl\/wp-json\/wp\/v2\/comments?post=1143"}],"version-history":[{"count":0,"href":"https:\/\/old-2023.weichie.com\/nl\/wp-json\/wp\/v2\/posts\/1143\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/old-2023.weichie.com\/nl\/wp-json\/wp\/v2\/media\/711"}],"wp:attachment":[{"href":"https:\/\/old-2023.weichie.com\/nl\/wp-json\/wp\/v2\/media?parent=1143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/old-2023.weichie.com\/nl\/wp-json\/wp\/v2\/categories?post=1143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/old-2023.weichie.com\/nl\/wp-json\/wp\/v2\/tags?post=1143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}