[{"data":1,"prerenderedAt":101},["Reactive",2],{"/samples/php":3},[4,62],{"_path":5,"_dir":6,"_draft":7,"_partial":7,"_locale":8,"title":9,"description":10,"language":11,"library":12,"link":13,"body":14,"_type":57,"_id":58,"_source":59,"_file":60,"_extension":61},"/samples/php/curl","php",false,"","Curl","cURL is commonly used as an extension to make HTTP requests and handle responses from within PHP scripts. It provides a wide range of features and options for making HTTP requests, including support for various protocols and authentication methods.","PHP","curl","https://www.php.net/manual/en/book.curl.php",{"type":15,"children":16,"toc":54},"root",[17,33,45],{"type":18,"tag":19,"props":20,"children":21},"element","p",{},[22,31],{"type":18,"tag":23,"props":24,"children":27},"a",{"href":13,"rel":25},[26],"nofollow",[28],{"type":29,"value":30},"text","cURL",{"type":29,"value":32}," is commonly used as an extension to make HTTP requests and handle responses from within PHP scripts. It provides a wide range of features and options for making HTTP requests, including support for various protocols and authentication methods.",{"type":18,"tag":34,"props":35,"children":39},"pre",{"className":36,"code":38,"language":6,"meta":8},[37],"language-php","\u003C?php\n\nfunction convert($api_key, $params, $endpoint = 'pdf') {\n    // Ensure the endpoint is valid\n    if (!in_array($endpoint, array('pdf', 'png', 'jpg', 'webp'))) {\n        throw new Exception('Invalid endpoint specified');\n    }\n\n    // Set the API URL\n    $url = 'https://api.pdfshift.io/v3/convert/' . $endpoint;\n\n    // Set cURL options\n    $curl = curl_init($url);\n    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);\n    curl_setopt($curl, CURLOPT_POST, true);\n    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params));\n    curl_setopt($curl, CURLOPT_HTTPHEADER, array(\n        'Content-Type: application/json',\n        'X-API-Key: '.$api_key\n    ));\n\n    // Execute the cURL request\n    $response = curl_exec($curl);\n\n    // Check for cURL errors\n    if ($response === false) {\n        $error_message = curl_error($curl);\n        curl_close($curl);\n        throw new Exception(\"cURL error: $error_message\");\n    }\n\n    // Get the HTTP status code\n    $http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);\n\n    // Close cURL resource\n    curl_close($curl);\n\n    // Check if the request was successful\n    if ($http_status !== 200) {\n        throw new Exception(\"Request failed with HTTP status code: $http_status\");\n    }\n\n    // Check if filename or webhook were passed\n    if (array_key_exists('filename', $params) || array_key_exists('webhook', $params)) {\n        // Decode the response JSON\n        return json_decode($response, true);\n    }\n\n    // Return the binary content\n    return $response;\n}\n",[40],{"type":18,"tag":41,"props":42,"children":43},"code",{"__ignoreMap":8},[44],{"type":29,"value":38},{"type":18,"tag":34,"props":46,"children":49},{"className":47,"code":48,"language":6,"meta":8},[37],"try {\n    $result = convert('sk_XXXXXXXXXXXXXX', array(\n        'source' => 'https://en.wikipedia.org/wiki/REST'\n    ));\n\n    var_dump($result);\n} catch (Exception $e) {\n    echo 'Error: ' . $e->getMessage();\n}\n",[50],{"type":18,"tag":41,"props":51,"children":52},{"__ignoreMap":8},[53],{"type":29,"value":48},{"title":8,"searchDepth":55,"depth":55,"links":56},2,[],"markdown","content:samples:php:curl.md","content","samples/php/curl.md","md",{"_path":63,"_dir":6,"_draft":7,"_partial":7,"_locale":8,"title":64,"description":65,"language":11,"library":66,"link":67,"body":68,"_type":57,"_id":99,"_source":59,"_file":100,"_extension":61},"/samples/php/guzzle","Guzzle","Guzzle is a popular PHP HTTP client library that provides a simple and flexible API for making HTTP requests and handling responses. It offers features like request and response middleware, streaming uploads and downloads, and request batching. Guzzle is widely used for consuming web services, interacting with APIs, and performing HTTP operations in PHP applications.","guzzle","https://docs.guzzlephp.org/en/stable/",{"type":15,"children":69,"toc":97},[70,80,89],{"type":18,"tag":19,"props":71,"children":72},{},[73,78],{"type":18,"tag":23,"props":74,"children":76},{"href":67,"rel":75},[26],[77],{"type":29,"value":64},{"type":29,"value":79}," is a popular PHP HTTP client library that provides a simple and flexible API for making HTTP requests and handling responses. It offers features like request and response middleware, streaming uploads and downloads, and request batching. Guzzle is widely used for consuming web services, interacting with APIs, and performing HTTP operations in PHP applications.",{"type":18,"tag":34,"props":81,"children":84},{"className":82,"code":83,"language":6,"meta":8},[37],"use GuzzleHttp\\Client;\nuse GuzzleHttp\\Exception\\RequestException;\n\nfunction convert($api_key, $params, $endpoint = 'pdf') {\n    // Ensure the endpoint is valid\n    if (!in_array($endpoint, array('pdf', 'png', 'jpg', 'webp'))) {\n        throw new Exception('Invalid endpoint specified');\n    }\n\n    // Set the API URL\n    $url = 'https://api.pdfshift.io/v3/convert/' . $endpoint;\n\n    // Create a Guzzle client\n    $client = new Client();\n\n    try {\n        // Make the POST request\n        $response = $client->post($url, [\n            'headers' => [\n                'Content-Type' => 'application/json',\n                'X-API-Key' => $api_key\n            ],\n            'json' => $params\n        ]);\n\n        // Get the response body\n        $body = $response->getBody()->getContents();\n\n        // Check if filename or webhook were passed\n        if (array_key_exists('filename', $params) || array_key_exists('webhook', $params)) {\n            return json_decode($body, true);\n        }\n\n        // Return the binary content\n        return $body;\n    } catch (RequestException $e) {\n        if ($e->hasResponse()) {\n            $statusCode = $e->getResponse()->getStatusCode();\n            $reasonPhrase = $e->getResponse()->getReasonPhrase();\n            throw new Exception(\"Request failed with status code $statusCode: $reasonPhrase\");\n        } else {\n            throw new Exception(\"Request failed: \" . $e->getMessage());\n        }\n    }\n}\n",[85],{"type":18,"tag":41,"props":86,"children":87},{"__ignoreMap":8},[88],{"type":29,"value":83},{"type":18,"tag":34,"props":90,"children":92},{"className":91,"code":48,"language":6,"meta":8},[37],[93],{"type":18,"tag":41,"props":94,"children":95},{"__ignoreMap":8},[96],{"type":29,"value":48},{"title":8,"searchDepth":55,"depth":55,"links":98},[],"content:samples:php:guzzle.md","samples/php/guzzle.md",1785426821577]