【问题标题】:eBay api search itemeBay api 搜索项
【发布时间】:2019-09-08 06:31:40
【问题描述】:

我想通过关键字搜索获得项目。

<?php
error_reporting(E_ALL);

require_once("includes/config.php");

// Construct the HTTP GET call
$apicall = "https://api.ebay.com/buy/browse/v1/item_summary/search?"; // 
URL to call
$apicall .= "q=GTR"; //search keyword
$apicall .= "limit=3"; 

// Create headers to send with CURL request.
$headers = array
(
'X-EBAY-API-APP-ID: ' . $sapp_id,
'X-EBAY-API-DEV-NAME: ' . $dev_id,
'X-EBAY-API-CERT-NAME: ' . $cert_id,
'X-EBAY-C-ENDUSERCTX: 5337984774'
);

// Send request to eBay and load response in $response
$connection = curl_init();
curl_setopt($connection, CURLOPT_URL, 
"https://api.ebay.com/buy/browse/v1/item_summary/search?");
curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($connection, CURLOPT_HTTPHEADER, $headers);
curl_setopt($connection, CURLOPT_POST, 1);
curl_setopt($connection, CURLOPT_POSTFIELDS, $apicall);
curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($connection);
curl_close($connection);
// Load the call and capture the document returned by eBay API
$resp = $response;

// Output response
echo("<pre>");

print_r($resp);
//To retrieve specific values of the array in PHP, you will need to use 
object operator
// $value = $resp->Item->ConvertedCurrentPrice;
// $value = $resp->Item->ConvertedCurrentPrice;
// print $xml->Product->Title;

echo("</pre>");

?>

我在运行时遇到了这个错误。

{
"errors": [
{
"errorId": 3001,
"domain": "ROUTING",
"category": "REQUEST",
"message": "Request Rejected",
"longMessage": "The Request has errors. For help, see the documentation for this API."
}
]
}

由于我是初学者,我不知道该怎么做。

我尝试了 xml 请求方法,它工作正常,但我想用 REST API 方法来做。

我关注这个文档 - Official eBay search api documentation

我想知道为什么我会收到此错误以及实现此搜索查询的正确方法是什么?

【问题讨论】:

  • 在您的curl_setopt($connection, CURLOPT_URL, 行中,您是否应该使用您之前构建的$apicall 的值?
  • 您构造的 url 看起来不正确 - 它应该更像 ..../search?q=GTR&amp;limit=3 而不是 ..../search?q=GTRlimit=3??
  • 查看文档developer.ebay.com/api-docs/buy/browse/resources/methods 建议要使用search 方法,您应该发出一个GET 请求,而您的curl 代码正在发送一个POST 请求。

标签: php ebay-api


【解决方案1】:

未测试,因为我没有 eBay 开发者帐户,但以下内容可能会有所帮助。下面的代码将发出一个 GET 请求,其中查询字符串作为 url 的一部分,正如 documentation 建议的那样

<?php

    error_reporting( E_ALL );

    require_once("includes/config.php");

    /* 
        you can download from https://curl.haxx.se/docs/caextract.html
    */
    $cacert='c:/wwwroot/cacert.pem'; # edit as appropriate to point to valid `cacert.pem` file
    $verbose=true;




    $endpoint = "https://api.ebay.com/buy/browse/v1/item_summary/search?";
    $args=array(
        'q'     =>  'GTR',
        'limit' =>  3
    );
    $url=sprintf( '%s%s', $endpoint, http_build_query( $args ) );

    /*
        for advanced debug information we create a stream
        and output to that during the request. At the end 
        of the request the stream is processed and we can
        view that data.
    */
    if( $verbose )$vbh = fopen('php://temp', 'w+');

    $headers = array(
        'X-EBAY-API-APP-ID: ' . $sapp_id,
        'X-EBAY-API-DEV-NAME: ' . $dev_id,
        'X-EBAY-API-CERT-NAME: ' . $cert_id,
        'X-EBAY-C-ENDUSERCTX: 5337984774'
    );

    $curl = curl_init();
    curl_setopt( $curl, CURLOPT_URL, $url );
    curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, true );
    curl_setopt( $curl, CURLOPT_CAINFO, $cacert );
    curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
    curl_setopt( $curl, CURLOPT_HTTPHEADER, $headers );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt( $curl, CURLOPT_USERAGENT, 'curl' );

    if( $verbose ){
        curl_setopt( $curl, CURLOPT_VERBOSE, true );
        curl_setopt( $curl, CURLOPT_NOPROGRESS, true );
        curl_setopt( $curl, CURLOPT_STDERR, $vbh ); # the stream object
    }

    /* make the request and get info */
    $response = curl_exec( $curl );
    $info=(object)curl_getinfo( $curl );
    $errors=curl_error( $curl );

    /* store the verbose debug info */
    if( $verbose ){
        rewind( $vbh );
        $debug=stream_get_contents( $vbh );
        fclose( $vbh );
    }
    curl_close( $curl );




    if( $info->http_code==200 ){

        printf( '<pre>%s</pre>', print_r( $response, 1 ) );

    } else{

        printf( '<pre>%s</pre>', print_r( $verbose ? $debug : $info ,1 ) );
        printf( '<pre>%s</pre>', print_r( $errors, 1 ) );
    }
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-13
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    • 2022-12-29
    • 1970-01-01
    • 1970-01-01
    • 2017-10-03
    相关资源
    最近更新 更多