0

I have been working on setting up the frase api. and created the following curl snippet.

<?php


    $url = 'http://api.frase.io/api/v1/process_url';
    //The data you want to send via POST
    
    $fields = ['url' => 'https://firstsiteguide.com/best-gaming-blogs/', 'token' =>  "dd528796a9924dae9962bc5bd7ccdb20"];

      $ch = curl_init();
      curl_setopt($ch,CURLOPT_URL,$url);
      curl_setopt($ch,CURLOPT_POST, 1);              
      curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($fields));
      curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_FAILONERROR, true);
      curl_setopt($ch,CURLOPT_CONNECTTIMEOUT ,3); 
      curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0');
      curl_setopt($ch,CURLOPT_TIMEOUT, 20);
      $response = curl_exec($ch);
      
      if (curl_errno($ch)) {
            $error_msg = curl_error($ch);
            echo "<br/>CURL ERROR: ". $error_msg ."<br/>";
      }else{
          print "curl response is:" . $response ;
      }

      
      curl_close ($ch);

?>

I am not sure why, But I am receiving the following error for the same

The requested URL returned error: 400 Bad Request

Can help me identify what part of code I am missing or doing wrong. Thank you so much in advance.

abhlpa
  • 3
  • 1
  • 3
  • 2
    You need to add the token as a header field, according to the frase doc. See https://stackoverflow.com/questions/8115683/php-curl-custom-headers – Don R Jul 07 '21 at 15:19
  • @DonR Thanks for the help! added the token in headers still the response is blank. – abhlpa Jul 07 '21 at 15:28

1 Answers1

0

You're passing the token as a body parameter instead of a header. The body parameter needs to be sent as a JSON encoded string as mentioned on the API documentation page. Also, you need to remove or at least increase the cURL timeout value as it takes time to fetch, process and return a value from the API end. Note that the API will return the response in JSON format.

So, the complete code should be as:

<?php

$url = 'http://api.frase.io/api/v1/process_url'; //The endpoint url you want to send data via POST

$headers = ['token: dd528796a9924dae9962bc5bd7ccdb20']; // add this line, headers

$fields = ['url' => 'https://firstsiteguide.com/best-gaming-blogs/']; // modify this line, body

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // add this line
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); // modify this
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0');

$response = curl_exec($ch);

if (curl_errno($ch))
{
    $error_msg = curl_error($ch);
    echo "<br/>CURL ERROR: " . $error_msg . "<br/>";
}
else
{
    print($response);
    
    // $values = json_decode($response, true); // this is an array with all the values
}

curl_close($ch);

?>
OMi Shah
  • 5,768
  • 3
  • 25
  • 34