0

curl を使用して、php から Web サービスを呼び出すための次のコードがあります。

<?php
echo "Init<br />";
$url = 'http://server-ip/applications/time2gate.aspx?x=1182&y=365&map=1002&gate=B3&mode=time2gate&session=5fdf288d-01b0-414a-ba2a-58d3f624e453';

$ch = curl_init($url);
echo "1";
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$resp = curl_exec($ch);

$status_code = array();
preg_match('/\d\d\d/', $resp, $status_code);

switch($status_code[0]) {
        case 200:
            echo "Success<br />";
            break;
    case 503:
            die('Your call to Web Service failed and returned an HTTP 503.');
            break;
    case 403:
            die('Your call to Web Service failed and returned an HTTP status of 403.');
            break;
    case 400:
            die('Your call to Web Services failed and returned an HTTP status of 400.');
            break;
    default:
            die('Your call to Web Services returned an unexpected HTTP status of:' .    $status_code[0]);
}

if(curl_errno($ch))
{
    echo 'error' . curl_error($ch);
}

curl_close($ch);

?>

問題は、163、815、329 などの HTTP 応答コードを受信することです...なぜこれが起こっているのですか? これらのコードは何を意味しますか? Apache のエラー ログを確認しましたが、コードにエラーは見られませんでした。また、提供された URL への呼び出しをテストしたところ、Mozilla の Poster Add-on で動作します。

何か案は?Ubuntu 12でphp 5を使用しています。

ありがとう、ニック

4

1 に答える 1

1

API 呼び出しを行う必要があるときは、GitHub で入手できるシンプルなライブラリを使用します: https://github.com/rmccue/Requests

このライブラリを使用する例を以下に示します。API からの完全な応答が出力されます。

<?php

require_once('library/Requests.php');

$url = 'http://server-ip/applications/time2gate.aspx?x=1182&y=365&map=1002&gate=B3&mode=time2gate&session=5fdf288d-01b0-414a-ba2a-58d3f624e453';

// Next, make sure Requests can load internal classes
Requests::register_autoloader();

// Now let's make a request!
$request = Requests::get($url, array('Accept' => 'application/json'));

echo '<pre>';
    print_r($request);
echo '</pre>';
于 2013-06-17T14:59:54.117 に答える