2012-09-27 4 views
2

제품 업데이트를위한 "PUT"방법에 문제가 있습니다. 나는 내가 읽은 404를 얻는다. 분명히 제품이 존재하지 않는다는 것을 의미한다. 연결이 잘되어있어 제품을 성공적으로 추가했습니다.제품 업데이트 : http_status_message => 찾을 수는 없지만 제품에는 존재합니다.

현재 내 범위는 내가 찾을 수있는 모든 것을 수행하도록 설정되어 있습니다. 은 dev가 완료된 후이를 되돌릴 것입니다. 나는 "ohShpify"PHP의 API 어댑터를 사용하고 /admin/products/#123456789.json http://api.shopify.com/product.html#update

:

var $scope = "write_products,write_content,write_themes,write_customers,write_orders,write_script_tags,write_shipping"; 

이 API는 업데이트 URL을 보여줍니다. 나는 GitHub에 대해 질문을하지만 댓글과 위키가 꺼진 것 같습니다. 클래스보고에 관심이있는 사람들을 위해 https://github.com/cmcdonaldca/ohShopify.php/blob/master/shopify.php

:

<?php class ShopifyClient { 
public $shop_domain; 
private $token; 
private $api_key; 
private $secret; 
private $last_response_headers = null; 

public function __construct($shop_domain, $token, $api_key, $secret) { 
    $this->name = "ShopifyClient"; 
    $this->shop_domain = $shop_domain; 
    $this->token = $token; 
    $this->api_key = $api_key; 
    $this->secret = $secret; 
} 

// Get the URL required to request authorization 
public function getAuthorizeUrl($scope, $redirect_url='') { 
    $url = "http://{$this->shop_domain}/admin/oauth/authorize?client_id={$this->api_key}&scope=" . urlencode($scope); 
    if ($redirect_url != '') 
    { 
     $url .= "&redirect_uri=" . urlencode($redirect_url); 
    } 
    return $url; 
} 

// Once the User has authorized the app, call this with the code to get the access token 
public function getAccessToken($code) { 
    // POST to POST https://SHOP_NAME.myshopify.com/admin/oauth/access_token 
    $url = "https://{$this->shop_domain}/admin/oauth/access_token"; 
    $payload = "client_id={$this->api_key}&client_secret={$this->secret}&code=$code"; 
    $response = $this->curlHttpApiRequest('POST', $url, '', $payload, array()); 
    $response = json_decode($response, true); 
    if (isset($response['access_token'])) 
     return $response['access_token']; 
    return ''; 
} 

public function callsMade() 
{ 
    return $this->shopApiCallLimitParam(0); 
} 

public function callLimit() 
{ 
    return $this->shopApiCallLimitParam(1); 
} 

public function callsLeft($response_headers) 
{ 
    return $this->callLimit() - $this->callsMade(); 
} 

public function call($method, $path, $params=array()) 
{ 
    $baseurl = "https://{$this->shop_domain}/"; 

    $url = $baseurl.ltrim($path, '/'); 
    $query = in_array($method, array('GET','DELETE')) ? $params : array(); 
    $payload = in_array($method, array('POST','PUT')) ? stripslashes(json_encode($params)) : array(); 
    $request_headers = in_array($method, array('POST','PUT')) ? array("Content-Type: application/json; charset=utf-8", 'Expect:') : array(); 

    // add auth headers 
    $request_headers[] = 'X-Shopify-Access-Token: ' . $this->token; 

    $response = $this->curlHttpApiRequest($method, $url, $query, $payload, $request_headers); 
    $response = json_decode($response, true); 

    if (isset($response['errors']) or ($this->last_response_headers['http_status_code'] >= 400)) 
     throw new ShopifyApiException($method, $path, $params, $this->last_response_headers, $response); 

    return (is_array($response) and (count($response) > 0)) ? array_shift($response) : $response; 
} 

private function curlHttpApiRequest($method, $url, $query='', $payload='', $request_headers=array()) 
{ 
    $url = $this->curlAppendQuery($url, $query); 
    $ch = curl_init($url); 
    $this->curlSetopts($ch, $method, $payload, $request_headers); 
    $response = curl_exec($ch); 
    $errno = curl_errno($ch); 
    $error = curl_error($ch); 
    curl_close($ch); 

    if ($errno) throw new ShopifyCurlException($error, $errno); 
    list($message_headers, $message_body) = preg_split("/\r\n\r\n|\n\n|\r\r/", $response, 2); 
    $this->last_response_headers = $this->curlParseHeaders($message_headers); 

    return $message_body; 
} 

private function curlAppendQuery($url, $query) 
{ 
    if (empty($query)) return $url; 
    if (is_array($query)) return "$url?".http_build_query($query); 
    else return "$url?$query"; 
} 

private function curlSetopts($ch, $method, $payload, $request_headers) 
{ 
    curl_setopt($ch, CURLOPT_HEADER, true); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
    curl_setopt($ch, CURLOPT_MAXREDIRS, 3); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
    curl_setopt($ch, CURLOPT_USERAGENT, 'HAC'); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 30); 

    curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, $method); 
    if (!empty($request_headers)) curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers); 

    if ($method != 'GET' && !empty($payload)) 
    { 
     if (is_array($payload)) $payload = http_build_query($payload); 
     curl_setopt ($ch, CURLOPT_POSTFIELDS, $payload); 
    } 
} 

private function curlParseHeaders($message_headers) 
{ 
    $header_lines = preg_split("/\r\n|\n|\r/", $message_headers); 
    $headers = array(); 
    list(, $headers['http_status_code'], $headers['http_status_message']) = explode(' ', trim(array_shift($header_lines)), 3); 
    foreach ($header_lines as $header_line) 
    { 
     list($name, $value) = explode(':', $header_line, 2); 
     $name = strtolower($name); 
     $headers[$name] = trim($value); 
    } 

    return $headers; 
} 

private function shopApiCallLimitParam($index) 
{ 
    if ($this->last_response_headers == null) 
    { 
     throw new Exception('Cannot be called before an API call.'); 
    } 
    $params = explode('/', $this->last_response_headers['http_x_shopify_shop_api_call_limit']); 
    return (int) $params[$index]; 
} } 
class ShopifyCurlException extends Exception { } 
class ShopifyApiException extends Exception { 
protected $method; 
protected $path; 
protected $params; 
protected $response_headers; 
protected $response; 

function __construct($method, $path, $params, $response_headers, $response) 
{ 
    $this->method = $method; 
    $this->path = $path; 
    $this->params = $params; 
    $this->response_headers = $response_headers; 
    $this->response = $response; 

    parent::__construct($response_headers['http_status_message'], $response_headers['http_status_code']); 
} 

function getMethod() { return $this->method; } 
function getPath() { return $this->path; } 
function getParams() { return $this->params; } 
function getResponseHeaders() { return $this->response_headers; } 
function getResponse() { return $this->response; } 
} 
?> 

답변

0

도랑 제품 ID에서 # 문자.

pseudo-ruby 구문이므로 문서에 포함되어 있으므로 /admin/products/#{id}.json/admin/products/123456789.json으로 변환됩니다.

+0

나는 그것을 또한 시도했다. 나는 그것을 테스트하기 위해 몇 가지 구성을 시도했다. 모두 똑같이 돌아옵니다. – vc27

+0

내 코드에 문제가 있음을 발견했습니다. 업데이트 할 제품에 "metafields"배열을 추가하고있었습니다. 분명히 "메타 필드 (metafields)"를 업데이트하는 것과 동시에 동시에 할 수없는 것이 있습니다. 아마도 하나의 업데이트가 아니라 두 번의 업데이트를 수행 할 것입니다. – vc27

관련 문제