有没有一种方法可以将ISBNdb-curl转换为php



我以前从未使用过curl,我想将其改写为在php中工作的内容。有简单的方法吗?

curl -X 'POST' 
'https://api2.isbndb.com/books' 
-H 'accept: application/json' 
-H 'Authorization: 47296_a0846561675961d9272454ea90b82240' 
-H 'Content-Type: application/json' 
-d 'isbns=9780738726243,9780738747842,9780738756707,9780738765457'

使用您的初始卷曲查询:

curl -X 'POST'
'https://api2.isbndb.com/books'
-H 'accept: application/json'
-H 'Authorization: 47296_a0846561675961d9272454ea90b82240'
-H 'Content-Type: application/json'
-d 'isbns=9780738726243,9780738747842,9780738756707,9780738765457'

-H开关表示header值,而-d表示POST请求主体本身。考虑到这一点,使用以下简单的功能和配置应该会产生所需的结果。

function curl( $url=NULL, $options=NULL, $headers=false ){
/*
Download a copy of CACERT.pem from
https://curl.haxx.se/docs/caextract.html

save to webserver and modify the $cacert variable
to suit - ensuring that the path you choose is
readable.
*/
$cacert='c:/wwwroot/cacert.pem';    #modify this path
$vbh = fopen('php://temp', 'w+');
/* Initialise curl request object */
$curl=curl_init();
if( parse_url( $url, PHP_URL_SCHEME )=='https' ){
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, true );
curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
curl_setopt( $curl, CURLOPT_CAINFO, $cacert );
}
/* Define standard options */
curl_setopt( $curl, CURLOPT_URL, trim( $url ) );
curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' );
curl_setopt( $curl, CURLOPT_MAXREDIRS, 10 );
curl_setopt( $curl, CURLOPT_ENCODING, '' );

/* enhanced debug */
curl_setopt( $curl, CURLOPT_VERBOSE, true );
curl_setopt( $curl, CURLOPT_NOPROGRESS, true );
curl_setopt( $curl, CURLOPT_STDERR, $vbh );

/* Assign runtime parameters as options to override defaults. */
if( isset( $options ) && is_array( $options ) ){
foreach( $options as $param => $value ) curl_setopt( $curl, $param, $value );
}

/* send additional headers with the request */
if( $headers && is_array( $headers ) ){
curl_setopt( $curl, CURLOPT_HTTPHEADER, $headers );
}
/* Execute the request and store responses */
$res=(object)array(
'response'  =>  curl_exec( $curl ),
'status'    =>  curl_getinfo( $curl, CURLINFO_RESPONSE_CODE ),
'info'      =>  (object)curl_getinfo( $curl ),
'errors'    =>  curl_error( $curl )
);
rewind( $vbh );
$res->verbose=stream_get_contents( $vbh );
fclose( $vbh );
curl_close( $curl );

return $res;
}

然后设置配置

$url='https://api2.isbndb.com/books';
# The POST BODY
$payload=array( 
'isbns' => '9780738726243, 9780738747842, 9780738756707, 9780738765457'
);
# an array of headers to send with the request. The above function processes this array as it is.
$headers=array(
'Accept: application/json',
'Authorization: 47296_a0846561675961d9272454ea90b82240',
'Content-Type: application/json'
);
# instruct curl to make a POST request and include the POST body
$options=array(
CURLOPT_POST        =>  true,
CURLOPT_POSTFIELDS  =>  $payload
);
# make the request & proceed if the response status is 200 OK
$res=curl( $url, $options, $headers );
if( $res->status==200 ){
$json=json_decode( $res->response );
# do interesting things with the data...
printf('<pre>%s</pre>',print_r($json));
}

最终产生这种反应:

stdClass Object
(
[total] => 2
[requested] => 4
[data] => Array
(
[0] => stdClass Object
(
[publisher] => Llewellyn Publications
[language] => en_US
[image] => https://images.isbndb.com/covers/78/42/9780738747842.jpg
[title_long] => 365 Days of Hoodoo: Daily Rootwork, Mojo & Conjuration
[dimensions] => Height: 7.9 inches, Length: 5.3 inches, Width: 1 inches
[pages] => 432
[date_published] => 2018-12-08T00:00:01Z
[authors] => Array
(
[0] => Bird, Stephanie Rose
)
[title] => 365 Days of Hoodoo: Daily Rootwork, Mojo & Conjuration
[isbn13] => 9780738747842
[msrp] => 19.99
[binding] => Paperback
[isbn] => 073874784X
)
[1] => stdClass Object
(
[publisher] => Llewellyn Publications
[title_long] => How To Use A Crystal: 50 Practical Rituals And Spiritual Activities For Inspiration And Well-being
[pages] => 312
[date_published] => 2018-11-08
[authors] => Array
(
[0] => Richard Webster
)
[title] => How To Use A Crystal: 50 Practical Rituals And Spiritual Activities For Inspiration And Well-being
[isbn13] => 9780738756707
[msrp] => 16.99
[binding] => Paperback
[isbn] => 0738756709
)
)
)

最新更新