Home > Mobile >  PHP Curl PUT stopps at curl_exec
PHP Curl PUT stopps at curl_exec

Time:09-23

I try to update a product via PUT-Request, but the Programm stopps executing at curl_exec. Since i dont even get a error or anything, iam kinda lost in the dark. I generated the code using this site: https://reqbin.com/ It worked for my GET- and POST-Requests just fine, but the PUT doesnt seem to work. When i send the PUT-Request using that site, it actually works, but not on my local workspace.

This is my code:

public function updateProduct($product) {
        $id = $product->{"id"};
        $sku = $product->{"sku"};
        $infiniteInventory = $product->{"infiniteInventory"};
        $images = $product->{"images"};
        $category = $product->{"category"};
        $adaptivePrice = $product->{"adaptivePrice"};
        $manualPrice = $product->{"manualPrice"};
    
        $url = "https://system-url.de/myId/products/".$id;

        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_PUT, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        $headers = array(
            "Authorization: Bearer ".$this->getToken(),
            "Content-Type: application/json",
        );
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

        $obj = [
            "sku"=> $sku,
            "title"=> [
                "de_DE"=> "Claude"
            ],
            "infiniteInventory"=> $infiniteInventory,
            "images"=> $images,
            "category"=> $category,
            "adaptivePrice"=> $adaptivePrice,
            "manualPrice"=> $manualPrice
        ];

        $obj = json_encode($obj);

        $data = <<<DATA
            $obj
        DATA;

        curl_setopt($curl, CURLOPT_POSTFIELDS, $obj);

        //for debug only!
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

        curl_getinfo($curl);
        
        $resp = curl_exec($curl);
        curl_close($curl);
        var_dump($resp);

        return;
    }

CodePudding user response:

Try doing your curl like this:

<?php
$url = "https://system-url.de/myId/products/".$id;
$curl = curl_init();
$obj = [
        "sku"=> $sku,
        "title"=> [
            "de_DE"=> "Claude"
        ],
        "infiniteInventory"=> $infiniteInventory,
        "images"=> $images,
        "category"=> $category,
        "adaptivePrice"=> $adaptivePrice,
        "manualPrice"=> $manualPrice
    ];

    $obj = json_encode($obj);

curl_setopt_array($curl, array(
  CURLOPT_URL => $url,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'PUT',
  CURLOPT_POSTFIELDS => $obj,
  CURLOPT_HTTPHEADER => array(
    'Authorization: Bearer ".$this->getToken()',
    'Content-Type: application/json'
  ),
));
$response = curl_exec($curl);

curl_close($curl);
echo $response;

notice that it is not the same as yours -- the : CURLOPT_CUSTOMREQUEST => 'PUT', or curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); <- different way of writing the curl array ... Also I believe that this : curl_setopt($curl, CURLOPT_PUT, true); is not needed.

  • Related