curl -X POST (php)-ThrowExceptions

Exception or error:

I have the following Curl command. How do I execute this in PHP?

curl -X POST "https://api.mystream.to/v1/remote-upload"
  -H "Authorization: ACCESS_TOKEN"
  -d "url=<URL>&path=<PATH?>"
How to solve:

So, here is the PHP code:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.mystream.to/v1/remote-upload');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "url=<URL>&path=<PATH?>");

$headers = array();
$headers[] = 'Authorization: ACCESS_TOKEN';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

So I used the following website to generate this: curl-to-PHP

Now if you want more of an explanation, here it goes.


  1. Setting the URL

    curl_setopt($ch, CURLOPT_URL, 'https://api.mystream.to/v1/remote-upload');

  2. Setting the body to what you had before

    curl_setopt($ch, CURLOPT_POSTFIELDS, "url=<URL>&path=<PATH?>");

  3. Setting the Headers, or your access token

    $headers[] = 'Authorization: ACCESS_TOKEN';
    $headers[] = 'Content-Type: application/x-www-form-urlencoded';
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
  4. Executing the command and storing the result

    $result = curl_exec($ch);

Leave a Reply

Your email address will not be published. Required fields are marked *