3 Replies Latest reply on Apr 8, 2018 8:19 PM by Sanford Whiteman

    REST API - can't update snippets

    Peter Mariner

      I can do all sorts of stuff to Landing page and file assets through the REST API + cURL + PHP, but I've spent the day unsuccessfully trying to update snippet content.

       

      I'm using the code on this page:

       

      REST-Sample-Code/UpdateSnippetContent.php at master · Marketo/REST-Sample-Code · GitHub

       

      With our credientials for $clientID, $clientSecret and $host.

       

      It fails silently, output from print_r($snippet->postData());   is empty and there's no update to the snippet content.

       

      If I dump the $url value, and copy / paste into a browser, I get the JSON for the snippet. This tells me the credientials, and snippet ID are correct.

       

      Before I dig any further, does this actually work any more? Or maybe this feature has been quietly retired?

       

      Thanks in advance...

       

       

        • Re: REST API - can't update snippets
          Sanford Whiteman

          What are the exact JSON request and response payloads? I doubt this is really failing silently. 

           

          Post them here, using the Advanced Editor's syntax highlighter to format them as JavaScript (the highlighter doesn't list JSON separately).

           

          It's a good sanity check to send the request via Postman (setting headers appropriately, of course) to have complete control over what's sent and debug-level visibility of what's received.

           

          In addition, I personally avoid PHP these days so it's going to be easier to get help by presenting the raw HTTP level, rather than getting bogged down in language/extension-specific stuff.

            • Re: REST API - can't update snippets
              Peter Mariner

              Thanks for the reply.

               

              Requests in Postman were fine, problem was it had to work in *some* backend language that would allow me to fetch some structured data and update the Marketo snippet with the content.

               

              There were two problems.

               

              First, the cURL request didn't return correctly without CURLOPT_SSL_VERIFYPEER set to false, and second, I needed to urlencode() the content instead of adding slashes.

               

              Here's my working function in PHP, in case it's of use to anyone else.

               

              function updateSnippet($url, $access_token, $content, $type) {
                  $ch = curl_init($url); // your Marketo API endpoint   
                  curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json'));
                  curl_setopt($ch, CURLOPT_POST, true);
                  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
                  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
                  curl_setopt($ch, CURLOPT_POSTFIELDS, "access_token=".$access_token."&type=".$type."&content=".urlencode($content));
                  curl_getinfo($ch);
                  $response = curl_exec($ch);
                  return $response;
              }
              
              $snippet_id = 99;
              $url = "https://xxx-yyy-zzz.mktorest.com/rest/asset/v1/snippet/".$snippet_id."/content.json";
              $content = "The new snippet content goes here!";
              $type = "HTML";
              // $access_token = your Marketo REST API access token
              
              $response = updateSnippet($url, $access_token, $content, $type);
              var_dump($response);
              
              
                • Re: REST API - can't update snippets
                  Sanford Whiteman

                  First, the cURL request didn't return correctly without CURLOPT_SSL_VERIFYPEER set to false,

                  Don't need this if your cert bundle is up-to-date. It's not a good practice to ignore the cert (in the rare event that Marketo's DNS gets hacked, for example).

                   

                  I needed to urlencode() the content instead of adding slashes.

                  Well sure -- it's an application/x-www-form-urlencoded payload.  I woulda pointed that out if you'd shown your original code.

                   

                  (Also, the $access_token and $type should technically be run through urlencode() as well.)