在PHP中使用cURL的PUT时遇到问题

时间:2013-06-10 15:11:28

标签: php

我在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);
    ?>

0 个答案:

没有答案