我尝试使用标准网址调用API。所有工作都完全直接来自浏览器。例如:
http://www.worldcat.org/webservices/catalog/search/sru?query=srw.su%3D%22Computer组织%22& startRecord = 101& maximumRecords = 100& wskey = 7Rn7E7osoeJeQURAiEO4GH74HZa6BLdt7eXahgxdvwnfO6Ph7za1OzU9M2zx0e9nuDHVO34b5HfnLuOw
http://www.worldcat.org/webservices/catalog/search/sru?query=srw.su%3D%22Computer engineering%22& startRecord = 101& maximumRecords = 100& wskey = 7Rn7E7osoeJeQURAiEO4GH74HZa6BLdt7eXahgxdvwnfO6Ph7za1OzU9M2zx0e9nuDHVO34b5HfnLuOw
但是当我使用cURL来做这件事的时候,我继续从没有附加wskey的API那里得到错误:
function curl_get_contents($url)
{
$ch = curl_init();
d($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
d($OCLCqueries);
foreach ($OCLCqueries as $OCLCquery) {
// echo "managed1";
$XMLdata = curl_get_contents($OCLCquery);
// echo "managed2";
}
我之前定义了$ OCLCqueries。它是一个包含URL调用值的数组。 d()是一个函数,我从一个安装的库调用,它是一个更复杂的var_dump()形式,基本上具有相同的目的(用作调试的断点),但以更易读的格式转储数据。
这是我的输出:
<body><h1>HTTP Status 400 - org.oclc.wskey.api.WSKeyException: WsKeyParam(wskey) not found in request</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>org.oclc.wskey.api.WSKeyException: WsKeyParam(wskey) not found in request</u></p><p><b>description</b> <u>The request sent by the client was syntactically incorrect (org.oclc.wskey.api.WSKeyException: WsKeyParam(wskey) not found in request).</u></p><HR size="1" noshade="noshade"><h3></h3></body>
如何解决这个问题?
答案 0 :(得分:0)
最初我认为失败的最可能原因是请求中缺少User-Agent
字符串,但在测试下面的代码时发现它存在与否没有区别所以我认为问题是cURL请求中使用的url格式,因为它已经编码。通过将baseurl和参数分开如下,它可以正常工作。
$url='http://www.worldcat.org/webservices/catalog/search/sru';
$params=array(
'query' => 'srw.su="Computer organization"',
'startRecord' => 101,
'maximumRecords' => 100,
'wskey' => '7Rn7E7osoeJeQURAiEO4GH74HZa6BLdt7eXahgxdvwnfO6Ph7za1OzU9M2zx0e9nuDHVO34b5HfnLuOw'
);
function curl_get_contents( $url=false, $params=array() ){
if( $url && !empty( $params ) ){
$url = $url . '?' . http_build_query( $params );
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0 );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0' );
$data = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
return (object)array(
'response' => $data,
'info' => $info
);
}
}
$data = curl_get_contents( $url, $params );
print_r( $data->response );
为了简化对main函数的调用,您可以创建一个这样的简单包装函数。
function getcatalog( $baseurl=false, $term=false, $start=1, $max=1, $key=false ){
if( $baseurl && $term && $key ){
$params=array(
'query' => 'srw.su="'.$term.'"',
'startRecord' => $start,
'maximumRecords' => $max,
'wskey' => $key
);
return curl_get_contents( $baseurl, $params );
}
}
$data = getcatalog( $url, 'Computer organization', 1, 100,'7Rn7E7osoeJeQURAiEO4GH74HZa6BLdt7eXahgxdvwnfO6Ph7za1OzU9M2zx0e9nuDHVO34b5HfnLuOw');
if( $data->info['http_code']==200 ){
print_r( $data->response );
}