1

我在 PHP 中为 REST API 执行 cURL PUT 时遇到问题。它说 401 Unauthorized 我对 php 很陌生,所以我不知道该怎么做,请帮助我。

<?php
$url = "https://app.linkemperor.com/api/v2/vendors/blasts/6949235.json";
$data = '{"blasts": {"links" : "http://doblajemexicano.com.mx/sitio/index.php?title=Keyword_Elite_,_The_Ferrari_Of_Search_phrases_Study
http://fi.caravanwiki.com/index.php/Keyword_Elite_,_The_Ferrari_Of_Keywords_and_phrases_Research"}}';


            $content = json_encode($data);
            $oneMB = 1024 * 1024;
            $fh = fopen('php://temp/maxmemory:$oneMB', 'r+');
            fwrite($fh, $content);
            rewind($fh);
            $curl = curl_init($url);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_USERPWD, "<api key>:x");
            curl_setopt($curl, CURLOPT_INFILE, $fh);
            curl_setopt($curl, CURLOPT_INFILESIZE, strlen($content));
            curl_setopt($curl, CURLOPT_PUT, true);
            curl_setopt($curl, CURLOPT_TIMEOUT, 10);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

            $json_response = curl_exec($curl);

            $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

            if ( $status != 201 ) {
                die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
            }


            curl_close($curl);

            $response = json_decode($json_response, true);
    ?>
4

0 回答 0