Fueling Your Coding Mojo

Buckle up, fellow PHP enthusiast! We're loading up the rocket fuel for your coding adventures...

Popular Searches:
64
Q:

How can I pass variable to check plagiarism in Copyleaks API using PHP.? Here is my Code. I have just implement API in php code yet

Hi everyone,

I'm currently working on a PHP project where I want to integrate Copyleaks API to check for plagiarism. However, I'm having trouble passing variables to the API call in my PHP code.

Here is my code:

```php
// My API Key
$apikey = 'your_api_key';

// My text to check for plagiarism
$text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';

$url = 'https://api.copyleaks.com/v3/education/submit/url';

$data = array(
'apikey' => $apikey,
'text' => $text
);

// Initialize cURL
$ch = curl_init();

// Set the cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

// Execute the API call
$response = curl_exec($ch);

// Close cURL connection
curl_close($ch);

// Handle the API response
if (!$response) {
echo 'Error: No response received.';
} else {
$result = json_decode($response, true);
if ($result && isset($result['response'])) {
$response = $result['response'];
echo "Plagiarism Check Result: " . $response['result'] . "\n";
echo "Plagiarism Percentage: " . $response['percentage'] . "%\n";
} else {
echo 'Error: Invalid response received from the API.';
}
}
```

I'm having trouble figuring out how to pass the `apikey` and `text` variables to the API. Can anyone please help me with this? Any suggestions or examples would be greatly appreciated.

Thanks in advance!

All Replies

haven.kerluke

Hey folks,

I've recently worked on integrating Copyleaks API with PHP, and I faced a similar issue with passing variables to the plagiarism check endpoint. Let me share my experience and what worked for me.

In your code, make sure you're sending the data as JSON in the cURL request instead of URL-encoded form data. Copyleaks API expects JSON data for the request payload. To achieve this, you can modify your code as follows:

php
// My API Key
$apikey = 'your_api_key';

// My text to check for plagiarism
$text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';

$url = 'https://api.copyleaks.com/v3/education/submit/url';

$data = array(
'apikey' => $apikey,
'text' => $text
);

// Convert data to JSON
$jsonData = json_encode($data);

// Initialize cURL
$ch = curl_init();

// Set the cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

// Execute the API call
$response = curl_exec($ch);

// Close cURL connection
curl_close($ch);

// Handle the API response... (remaining code here)


By converting the data array to JSON using `json_encode()` and setting the appropriate `Content-Type` header in the cURL request, you'll be able to pass the variables correctly to the Copyleaks API.

Once you've made this adjustment, you should receive the expected plagiarism check results in the API response.

If you have any further queries or need assistance, feel free to ask. Good luck with your project!

trycia10

Hey there,

I've worked with Copyleaks API in PHP before, so I'd be happy to help you out! Looking at your code, it seems you're on the right track. To pass the `apikey` and `text` variables correctly, there are a few modifications you can make in your code.

First, make sure you have a valid API key from Copyleaks. It seems like you're assigning it to the `$apikey` variable correctly.

Next, instead of sending the `text` parameter directly in the `data` array, you should pass it as an array of arrays. Copyleaks API expects an array of `texts` with each item having a `text` property. Here's an example modification to your code:

php
// My text to check for plagiarism
$text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';

$data = array(
'apikey' => $apikey,
'texts' => array(
array('text' => $text)
),
);


This way, you can pass multiple texts for plagiarism checking if needed. The response from Copyleaks will then provide detailed results for each text you submitted.

Apart from that, your code looks fine, and once you make this adjustment, you should be able to pass the variables correctly and get the desired plagiarism check results.

Let me know if you have any further questions or need more help.

New to LearnPHP.org Community?

Join the community