How come "Connection" is misspelled in HTTP response header? - http

We're running the following php code:
function downloadwebsite($url, $nobody=false) {
$ch = curl_init();
// FLAGS
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOBODY, $nobody);
curl_setopt($ch, CURLOPT_HEADER, $nobody);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
$website = downloadwebsite("www.imdb.com", true);
echo nl2br($website);
At one line in the response it says: "Cneonction: close".
Why is it misspelled? Is it on purpose?

According to this StackOverflow thread, it's done on purpose for load balancers. It's done to help with keepalives, it seems.

Related

OpenAI API null response [duplicate]

I am getting an error for the following PHP code:
$curl = curl_init("https://api.openai.com/v1/engines/davinci/completions");
$data = array(
'prompt' => 'how many sundays in 2023',
'max_tokens' => 256,
'temperature' => 0.7,
'model' => 'text-davinci-003'
);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Authorization: Bearer sk-MY-API-KEY']);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
$result = curl_exec($curl);
curl_close($curl);
$result = json_decode($result);
print $result->choices[0]->text;
I correctly provided the API Key, but getting this error:
Error message: You didn't provide an API key. You need to provide your API key in an Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR_KEY)
All Engines endpoints are deprecated.
This is the correct Completions endpoint:
https://api.openai.com/v1/completions
Working example
If you run php test.php in CMD, the OpenAI API will return the following completion:
string(23) "
This is indeed a test"
test.php
<?php
$ch = curl_init();
$url = 'https://api.openai.com/v1/completions';
$api_key = 'sk-xxxxxxxxxxxxxxxxxxxx';
$post_fields = '{
"model": "text-davinci-003",
"prompt": "Say this is a test",
"max_tokens": 7,
"temperature": 0
}';
$header = [
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key
];
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
}
curl_close($ch);
$response = json_decode($result);
var_dump($response->choices[0]->text);
?>

Filter posts tags by wordpress api

I created a post-type called "material", with taxonomies of type tags called "ano". I'm trying to filter through the wordpress rest api a specific value for that "ano (years)", but it doesn't work, it returns all posts, including those that don't have the specific tag.
<?php
$url = 'https://cat.site.com/wp-json/wp/v2/material?filter[taxonomy]=ano&filter[term]=1994';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
var_dump($response);
?>
I found the solution, I used the tag ID:
https://cat.site.com/wp-json/wp/v2/material?ano=28

FCM push notification in laravel

I'm trying to send push notification in laravel 8 using Curl in following way :
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: key='. $serverKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $arrayToSend);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
//Send the request
$response = curl_exec($ch);
//Close request
if ($response === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
return $response;
everything is working fine I'm getting the notification.
Issue here is I want to send it in bulk to around 1 Million users, In laravel 8 can we send it in bulk?
Thank you
R

Why am I getting CLIENT_ERROR when uploading a photo to LinkedIn API?

I'm working on making an Image Share with the LinkedIn v2 api. There are three steps, according to the LinkedIn docs.
Register your image to be uploaded.
Upload your image to LinkedIn.
Create the image share.
After I complete step 2, I check the status of the upload with /v2/assets/{asset-id} and get a "CLIENT_ERROR". I have no idea what this means and haven't found much about it in the LinkedIn docs or online. It may have something to do with uploading a binary image file as LinkedIn asks, but as far as I know I am uploading one.
Edit: The php-curl I'm using to upload the image is below. The $uploadUrl is obtained from the image register (step 1.)
$data = [
'file' => curl_file_create($file, $mimeType)//;
];
ob_start();
$out = fopen('php://output', 'w');
$ch = curl_init($uploadUrl);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, $out);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch,CURLOPT_USERAGENT,'curl/7.35.0');
$authorizationHeader = trim("Authorization: Bearer $accessToken");
curl_setopt($ch, CURLOPT_HTTPHEADER, array($authorizationHeader,"Content-Type: {$mimeType}","X-Restli-Protocol-Version: 2.0.0"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
curl_setopt($ch, CURLOPT_UPLOAD, '1L');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
fclose($out);
$debug = ob_get_clean();
print_r($debug);
$resp_obj = json_decode($response);
print_r($response);
curl_close($ch);
The assets API is returning:
> GET /v2/assets/{redacted} HTTP/1.1
User-Agent: curl/7.35.0
Host: api.linkedin.com
Accept: */*
Authorization: Bearer {redacted}
< HTTP/1.1 200 OK
< X-LI-ResponseOrigin: RGW
< Content-Type: application/json
< X-RestLi-Protocol-Version: 1.0.0
< Content-Length: 319
< Date: Wed, 20 Mar 2019 14:09:18 GMT
< X-Li-Fabric: prod-ltx1
< Connection: keep-alive
< X-Li-Pop: prod-edc2-nkernB
< X-LI-Proto: http/1.1
< X-LI-UUID: {redacted}
< Set-Cookie: {redacted}
< X-LI-Route-Key: {redacted}
Response object is:
(
[serviceRelationships] => Array
(
[0] => stdClass Object
(
[identifier] => urn:li:userGeneratedContent
[relationshipType] => OWNER
)
)
[recipes] => Array
(
[0] => stdClass Object
(
[recipe] => urn:li:digitalmediaRecipe:feedshare-image
[status] => CLIENT_ERROR
)
)
[mediaTypeFamily] => STILLIMAGE
[created] => 1553090957146
[lastModified] => 1553090958505
[id] => {redacted}
[status] => ALLOWED
)
Update: it works fine when I upload the image using command line curl:
curl -i --upload-file {file} --header "Authorization: Bearer {auth}" {url}
Update: Solution:
use file_get_contents: curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents({path-to-your-image));**
$ch = curl_init($uploadUrl);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch,CURLOPT_USERAGENT,'curl/7.35.0');
$authorizationHeader = trim("Authorization: Bearer $accessToken");
curl_setopt($ch, CURLOPT_HTTPHEADER, array($authorizationHeader,"Content-Type: {$mimeType}","X-Restli-Protocol-Version: 2.0.0"));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents({path-to-your-image));
$response = curl_exec($ch);
This: curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents({path-to-your-image));
$ch = curl_init($uploadUrl);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch,CURLOPT_USERAGENT,'curl/7.35.0');
$authorizationHeader = trim("Authorization: Bearer $accessToken");
curl_setopt($ch, CURLOPT_HTTPHEADER, array($authorizationHeader,"Content-Type: {$mimeType}","X-Restli-Protocol-Version: 2.0.0"));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents({path-to-your-image));
$response = curl_exec($ch);
add headers to your code as Content-Type:application/octet-stream
Authorization': Bearer ${ access_token },
'X-Restli-Protocol-Version': '2.0.0',
'Content-Type': 'image/jpg'
body is simply the image file contents or a BLOB
method: POST or PUT... i guess one of it works with some and the second for others

HTTP response header code ignored when using cURL

I have the below code but for some reason I cannot get the correct HTTP response code as it always comes back as 0. I have tried testing a page that only has "header("HTTP/1.1 404 Not Found");" and run cURL on this page bit still I get 0. When I go direct to the page in the browser, the header code is correct. Any ideas?
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, Array (
"Content-Type: " . $content_type
));
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPGET, TRUE);
curl_setopt($curl, CURLOPT_USERAGENT, RESTClient :: USER_AGENT);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_HTTPHEADER, TRUE);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
curl_setopt($curl, CURLOPT_USERPWD, $auth);
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
echo $httpcode;
$result = curl_exec($curl);
curl_close($curl);
Naturally, the return code can't be retrieved until after the request has been performed, which happens with the curl_exec() call. Move your curl_getinfo after that line and it should work.

Resources