Error when requesting an API

Hi!

when you run the following code:

<?php
    $name = ["7ca47d86d0f56fe45ad01edb6c4c7771"]; // its ZoneID
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://api.cloudflare.com/client/v4/zones/$name/settings/ipv6');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');

    curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"value\":\"off\"}");

    $headers = array();
    $headers[] = 'X-Auth-Email: MAIL';
    $headers[] = 'X-Auth-Key: TOKEN';
    $headers[] = 'Content-Type: application/json';
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

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

    var_dump($result);

    curl_close($ch);
    ?>

I get in response:

"{“success”:false,“errors”:[{“code”:7000,“message”:“No route for that URI”}],“messages”:[],“result”:null}"

Where is my mistake ?

Thats not really a Cloudflare issue, rather a PHP issue.

If you had printed the URL you are requesting you’d have noticed you are requesting https://api.cloudflare.com/client/v4/zones/$name/settings/ipv6, which obviously wont work.

You probably want to place that string under double quotes rather than single ones.

I don’t quite understand. Would you mind showing me my mistake?

I mentioned it in my last sentence, please re-read that.

Also, that variable is not a string but an array. However at this point we really better hand that over to StackOverflow as it really is not Cloudflare related.

1 Like

I wrapped the string in " and got the following:

“{“success”:false,“errors”:[{“code”:7003,“message”:“Could not route to /zones/Array/settings/ipv6, perhaps your object identifier is invalid?”},{“code”:7000,“message”:“No route for that URI”}],“messages”:,“result”:null}”

Please re-read my responses.

And again, that is a question for Stack Overflow, not here, I am afraid.

I did as you answered and put double quotes

<?php
$name = ["7ca47d86d0f56fe45ad01edb6c4c7771"];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.cloudflare.com/client/v4/zones/$name/settings/ipv6");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');

curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"value\":\"off\"}");

$headers = array();
$headers[] = 'X-Auth-Email: {redated}';
$headers[] = 'X-Auth-Key: {redated}';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

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

var_dump($result);

curl_close($ch);
?>

You didnt, otherwise you wouldnt print an array.

But, sorry, for the third time, this here is not the right place for that question. Please ask over at StackOverflow.

Nothing is clear. They didn’t answer either

Ehm, you posted that 22 minutes ago

Seriously, wait a bit, respectively just re-read what I already wrote here.

And one of the responses you got is actually quite spot on

What have you tried to debug your problem?

I am Russian and communicate with you through a translator and probably get a wrong translation. In addition, my knowledge of php is insufficient. I really don’t know where my mistake is.

Change the call to the following and it will work.

curl_setopt($ch, CURLOPT_URL, 'https://api.cloudflare.com/client/v4/zones/7ca47d86d0f56fe45ad01edb6c4c7771/settings/ipv6');

Thanks! I know what works. So the thing is that I need to get ZoneID from the form

Alright, now we really reached a point that is way beyond the scope of the forum here. The forum here is for Clouflare related issues, not general PHP development questions.

Please just respond to the question on Stack Overflow and wait until you get responses or use a Russian PHP forum, should that be easier.

1 Like