你如何在php中调用API?

时间:2014-09-26 01:27:10

标签: php curl

我知道这已被多次回答。但是,在我的情况下,我需要首先与主机连接然后建立隧道。我只是从另一个复制了这个。这是:

$fp = ssh2_connect('xxx.xxx.xx.xxx', xxxx);

if (ssh2_auth_password($fp, 'username', 'password')) {
            echo "Authentication Successful!<br>";
            $connection = ssh2_tunnel($fp, 'xxx.xx.xx.xx', xx);

            if($connection){
                echo "Authentication Successful!<br>";

                $service_url ='Sample URL';

                $curl = curl_init();
                curl_setopt($curl, CURLOPT_URL, $service_url);
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1) or die("Curl Error TRANSFER");
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE) or die("Curl Error SSL");
                curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);

                $curl_response = curl_exec($curl);
                echo $curl_response."<br><br>";

                if ($curl_response === false) {
                    $info = curl_getinfo($curl);
                    echo curl_getinfo($curl) . '<br/>';
                    echo curl_errno($curl) . '<br/>';
                    echo "Curl Error: ".curl_error($curl) . '<br/>';

                    curl_close($curl);
                    die('error occured during curl exec. Additional info: ' . var_export($info));
                }
                else{
                    echo "Success";
                }

            } else {
                die('Authentication Failed...');
            }
                } else {
                die('Authentication Failed...');
}

1 个答案:

答案 0 :(得分:0)

They said it was suppose to work.
你有没有尝试过修理它?这里的大多数人都不喜欢抓住一些代码然后寻求帮助而不知道发生了什么。

此外......您需要提供有关错误内容的更多信息。 脚本返回一个值有几个位置......它在哪里返回? Authentication Failed...Curl Error:error occured during curl exec,还是别的什么?

我刚刚在我的服务器上运行了这个。没有问题。