PHP CURL Web服务调用


PHP CURL Web Service Call

我有以下代码,以便从php调用Web服务,使用curl:

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

问题是我收到HTTP响应代码,如163,815,329…为什么会发生这种情况?这些代码是什么意思?我检查了Apache的错误日志,在我的代码中没有看到任何错误。此外,我测试了对提供的url的调用,它可以与Mozilla的Poster插件一起工作。

任何想法?我在Ubuntu 12上使用php 5。

谢谢你,尼克

当我需要进行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>';