2

我正在尝试获取关键字的搜索产品

我的代码:

$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 时,它会显示“未找到操作”

这是屏幕截图

任何帮助,将不胜感激。

4

2 回答 2

2

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

只需用 GET 请求替换您的 POST 请求,一切都应该没问题

$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);
于 2017-01-12T09:50:33.683 回答
0
            <?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);
            ?>
于 2017-07-19T13:50:44.743 回答