Why is my PHP/Curl draft order request returning blank?

Solved

Why is my PHP/Curl draft order request returning blank?

aimtechnology
Visitor
2 1 0

so i can make this request successfuly  using an online curl sender but when converted to php below it returns blank. can you see anything wrong here ?

 

$data  = array(
        'draft_order' => [
            'line_items' => [
                [
                    "title" => "Pre payment for " . $custDetails['email'] . "- Repair order #" . $custDetails['serial'],
                    "price" => "100.00",
                    "quantity" => 1,
                ]
            ],
            'customer' => null,
        ]
    );

    $url = my address
    $headers = [
        'Content-Type: application/json',
        'X-Shopify-Access-Token: my token',
    ];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');


    $response = curl_exec($ch);
    if (curl_errno($ch)) {
        $error_msg = curl_error($ch);
    }
    curl_close($ch);
   
    if (isset($error_msg)) {
        // Handle error here
        return $error_msg;
    }


    $decodedResponse =  json_decode($response, true);
    if (isset($decodedResponse['draft_order']) && isset($decodedResponse['draft_order']['invoice_url'])) {
        return $decodedResponse['draft_order']['invoice_url']; // Return the draft order number (ID)
    } else {
        // Handle the case where the response does not contain the expected data
        return ':(';
    }
Accepted Solution (1)

aimtechnology
Visitor
2 1 0

This is an accepted solution.

I fixed this, the customer, although null required  it set out as a null id 

'customer' => ["id" => null]
 
also, I think because im primarily a js developer, i wasn't thinking how to access the invoice_url correcty!!:

// Separate headers and body
    list($headers, $body) = explode("\r\n\r\n", $response, 2);

    // Decode the JSON body
    $decodedResponse = json_decode($body);
// return the url
return $decodedResponse->draft_order->invoice_url;
 

View solution in original post

Reply 1 (1)

aimtechnology
Visitor
2 1 0

This is an accepted solution.

I fixed this, the customer, although null required  it set out as a null id 

'customer' => ["id" => null]
 
also, I think because im primarily a js developer, i wasn't thinking how to access the invoice_url correcty!!:

// Separate headers and body
    list($headers, $body) = explode("\r\n\r\n", $response, 2);

    // Decode the JSON body
    $decodedResponse = json_decode($body);
// return the url
return $decodedResponse->draft_order->invoice_url;