使用PHP发布到Shopify

时间:2015-05-04 15:44:31

标签: php json shopify

我试图从PHP页面发布到Shopify以表明订单项已经完成。我一定不能这样做,因为我一直得到一个null结果。我是在一个被取消的测试订单上这样做的,但我希望我至少得到某种非空响应,如果这是问题。这是我的代码:

$API_KEY = 'my-api-key';
$SECRET = 'my-shared-secret';
$PASS = 'my-shopify-password';
$STORE_URL = 'mydomain.myshopify.com';
$ITEM_ID = 'my-item-id';
$ORDER_ID = 'my-order-number';
$TRACKING_NUMBER = '12345';
$TRACKING_URL = 'http:\/\/test.com\/testurl';

$baseUrl = 'https://'.$API_KEY.':'.$PASS.'@'.$STORE_URL.
           '/admin/orders/'.$ORDER_ID.'/fulfillments.json';

$data = array('fulfillment' =>
                array(
                    'tracking_number' => $TRACKING_NUMBER,
                    'tracking_company' => 'USPS',
                    'tracking_url' => $TRACKING_URL,
                    'line_items' =>
                        array(
                            'id'=>$ITEM_ID,
                            ),
                ),
            );
$session = curl_init( $baseUrl );
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($session, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false);

$response = curl_exec($session);
curl_close($session);

$json = json_decode( $response, true );

var_dump($json);

3 个答案:

答案 0 :(得分:0)

你正在POST部分发送一个json,你确定这是你真正想做的吗?发布数据应该更像这样,发送data1=test1&data2=test2source

<?php
$API_KEY = 'my-api-key';
$SECRET = 'my-shared-secret';
$PASS = 'my-shopify-password';
$STORE_URL = 'mydomain.myshopify.com';
$ITEM_ID = 'my-item-id';
$ORDER_ID = 'my-order-number';
$TRACKING_NUMBER = '12345';
$TRACKING_URL = 'http:\/\/test.com\/testurl';

$baseUrl = 'https://'.$API_KEY.':'.$PASS.'@'.$STORE_URL.'/admin/orders/'.$ORDER_ID.'/fulfillments.json';

$fields = array(
        'lname' => urlencode($last_name),
        'fname' => urlencode($first_name),
        'title' => urlencode($title),
        'company' => urlencode($institution),
        'age' => urlencode($age),
        'email' => urlencode($email),
        'phone' => urlencode($phone)
);

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($session, CURLOPT_URL, $baseUrl);
curl_setopt($session, CURLOPT_POST, count($fields));
curl_setopt($session, CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);


var_dump($result);
//close connection
curl_close($ch);
?>

如果你真的想通过POST字段发送JSON,可能你错过了数据的长度:source

curl_setopt($, , CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data))                                                                       
);                  

答案 1 :(得分:0)

你不应该使用complete.json而不是???我可能会误解,但听起来你试图完成一项未决的履行。

/admin/orders/#{id}/fulfillments/#{id}/complete.json

https://docs.shopify.com/api/fulfillment#complete

答案 2 :(得分:0)

我发现了问题。 Shopify要求订单项包含其他数组,而不是:

'line_items' =>
                    array(
                        'id'=>$ITEM_ID,
                        )

应该是这样的:

'line_items' =>
                    array(
                       array(
                           'id'=>$ITEM_ID,
                        ) 
                    )