【问题标题】:Walmart API Search Products沃尔玛 API 搜索产品
【发布时间】:2017-01-12 09:38:19
【问题描述】:

我正在尝试为某个关键字获取搜索产品

我的代码:

$searchquery = "ipod";
$api_endpoint = "http://api.walmartlabs.com/v1/search";
$postfields = "apiKey=". $appid ."&query=" . $searchquery;
//$postfields = array('apiKey' => $appid, 'query' => $searchquery);
$connection = curl_init();
curl_setopt($connection, CURLOPT_URL, $api_endpoint);
curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 0);
//curl_setopt($connection, CURLOPT_HTTPHEADER, $headers);
curl_setopt($connection, CURLOPT_POST, true);
curl_setopt($connection, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($connection, CURLOPT_HEADER, true);
//curl_setopt($connection, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($connection, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($connection);
curl_close($connection);
print_r($api_endpoint);

print_r($response);

当我进入浏览器并访问 api.walmartlabs.com/v1/search?apiKey={appid}&query=ipod 时,它会显示结果,但是当我尝试使用 curl 时,它会显示 “未找到操作”

这里是Screenshot

任何帮助将不胜感激。

【问题讨论】:

    标签: php php-curl walmart-api


    【解决方案1】:

    查看文档 (https://developer.walmartlabs.com/io-docs) 似乎服务器需要一个 GET 请求。

    只需将您的 POST 请求替换为 GET 请求,一切都会好起来的

    $searchquery = "ipod";
    $api_endpoint = "http://api.walmartlabs.com/v1/search";
    $urlParams = "apiKey=". $appid ."&query=" . $searchquery;
    
    $fullUrl = $api_endpoint . '?' . $urlParams;
    
    $connection = curl_init();
    curl_setopt($connection, CURLOPT_URL, $fullUrl);
    curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($connection, CURLOPT_HEADER, true);
    curl_setopt($connection, CURLOPT_RETURNTRANSFER, true);
    
    $response = curl_exec($connection);
    curl_close($connection);
    
    print_r($api_endpoint);
    print_r($response);
    

    【讨论】:

    • 谢谢,我不敢相信我会犯这个错误,非常感谢!
    • @Jack 不客气 :) 如果您的问题得到解决,请不要忘记接受分析器
    【解决方案2】:
                <?php
                ini_set('display_errors', 1);
                //API URL : https://affiliates.walmart.com/#!/api
    
                $api_key="**********";  //https://developer.walmartlabs.com/apps/mykeys
                $keywords="men%20watches";  // Search text - whitespace separated sequence of keywords to search for
                $format="json";  // data we want in response
                $responseGroup="base";  // Specifies the item fields returned in the response, allowed response groups are [base, full]. Default value is base.
                $sort='price';  //Sorting criteria, allowed sort types are [relevance, price, title, bestseller, customerRating, new]. Default sort is by relevance.
                $order="asc";  //Sort ordering criteria, allowed values are [asc, desc]. This parameter is needed only for the sort types [price, title, customerRating].
                //http://api.walmartlabs.com/v1/search?apiKey={apiKey}&lsPublisherId={Your LinkShare Publisher Id}&query=ipod
                $request_url="http://api.walmartlabs.com/v1/search?apiKey=".$api_key."&query=".$keywords."&format=".$format."&responseGroup=".$responseGroup."&sort=".$sort."&order=".$order;
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL,$request_url);
                    curl_setopt($ch, CURLOPT_FAILONERROR,1);
                    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
                    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
                    $retValue = curl_exec($ch);       
                    // Check for errors and display the error message
                    if($errno = curl_errno($ch)) {
                        $error_message = curl_strerror($errno);
                        echo "cURL error ({$errno}):\n {$error_message}";
                    }   
                    curl_close($ch);
    
                    $arr = json_decode($retValue,true);
                    echo "<pre>";
                    print_r($arr);
                    // echo "<pre>";
                    // var_dump($retValue);
                ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-21
      • 2021-02-03
      相关资源
      最近更新 更多