如何在guzzle 3.9中设置CURLOPT_USERPWD

时间:2016-01-20 22:08:47

标签: guzzle

我正在尝试使用guzzle 3.9迁移以下内容以关注restful Web服务

$url = Input::get('company');
            $fid='/tradingAccounts';//sample fid
            $url = $url.$fid;
            $name = Input::get('username');
            $password = Input::get('password');
            $baseurl = $url.'?&format=json';

            //SAGE USERNAME & PASSWORD
            $username = $name;
            $password = $password;


            //initiaize
            $ch = curl_init();

            //set options
            curl_setopt($ch, CURLOPT_URL, $baseurl);
            curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
            curl_setopt($ch, CURLOPT_HTTPGET, true);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            //execute
            $result = curl_exec($ch);

            //close curl session / free resources
            curl_close($ch);

            // doing a substring as there are 3 weird characters being passed back from IIS in front of the string
            $json = json_decode(substr($result, 3, strlen($result)), true);

这段代码工作正常,但我想使用一种更安静的方法我尝试了几种不同的方法来做到这一点但我一直得到[状态代码] 401 [原因短语]未经授权这是我试过的

    $username = "MANAGER";
    $password = "";

    $client = new \Guzzle\Service\Client('http://localhost:5493/sdata/accounts50/GCRM/');
    //$client->getCurlOptions()->set(CURLOPT_USERPWD, $username . ":" . $password );
    $request = $client->get("-/$usernames");
    $request->setAuth('username', 'password');
    echo $request->getUrl();
    $response = $request->send();

    dd($response);

0 个答案:

没有答案