Take out the $headers variable from the function - variables

I have a problem when I want to fetch the $header variabel to get the header response
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $u);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_HEADERFUNCTION,
function ($curl, $header) use (&$headers) {
$len = strlen($header);
$header = explode('Set-Cookie: ', $header, 2);
if (count($header) < 2) // ignore invalid headers
return $len;
$headers[strtolower(trim($header[0]))][] = trim($header[1]);
print_r($headers);
return $len;
});
I want to get the $headers variable out,and manipulate it

Related

Whatsapp Cloud Api get Media Object ID

I want to upload the media via Whatsapp cloud API in order to send the media to WhatsApp, I have followed the link below, but having the stated issue.
https://developers.facebook.com/docs/whatsapp/cloud-api/reference/media#get-media-id
Array ( [error] => Array ( [message] => (#100) The parameter messaging_product is required. [type] => OAuthException [code] => 100 [fbtrace_id] => AQPftb9Bf343375gQ_QytxNen ) ) 400
My Code is
// #1640191015925.jpg on my root folder
$post = [
'file' => '#1640191015925.jpg;type=image/jpeg',
'messaging_product' => 'whatsapp'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/v13.0/Phone-Number-ID/media');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$headers = array();
$YOUR_WHATSAPP_ACCESS_TOKEN = '+++++++';
$headers[] = "Authorization: Bearer $YOUR_WHATSAPP_ACCESS_TOKEN";
$headers[] = "Content-Type: image/jpeg";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = json_decode(curl_exec($ch), true);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//$MEDIA_OBJECT_ID = $result['media'][0]['id']; //MEDIA OBJECT ID
print_r($result);
print_r($httpcode);
?> ```
I had the same issue, after some digging around I wrote this function to upload a file and return the id.
function waFileUploader($token, $file, $senderid){
$mime = mime_content_type($file);
$info = pathinfo($file);
$name = $info['basename'];
$curlfile = new CURLFile($file, $mime, $name);
$filedata = array("messaging_product"=>"whatsapp", "file" => $curlfile);
$url = 'https://graph.facebook.com/v13.0/' . $senderid . '/media';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER,array("Authorization: Bearer $token", "Content-Type: multipart/form-data"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $filedata);
$r = curl_exec($ch);
curl_close($ch);
$id = json_decode($r, true);
return $id["id"];
}

How to change `curl_multi_init` single thread to multi thread

Ask experts how to change to curl_multi_init multi-threaded?
My idea is to split the $data array into array_chunk($data, 15, true); and then do CURL, but I'm a novice, I don't understand the examples on the Internet, please help
Simulate 100 pieces of data:
function post($url, $data = '', $head = 'application/x-www-form-urlencoded')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type:{$head};charset=utf-8;"));
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
if (!empty($data)) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
$url = 'http://localhost/test.php';
$res = [];
for ($i=0; $i < 100; $i++) {
$res[$key] = post($url, 'key='.$i);
}
var_dump($res);
The code below, I know it must be wrong, please correct me, thank you!!
$newData = array_chunk($data, 10, true);
foreach ($newData as $k=> $tmp) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type:application/x-www-form-urlencoded;charset=utf-8;"));
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$mh = curl_multi_init();
foreach ($tmp as $key => $value) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $tmp);
curl_multi_add_handle($mh, $ch);
}
$active = null;
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc == CURLM_OK) {
if (curl_multi_select($mh) != -1) {
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
$res[$k] = curl_multi_getcontent($ch);
curl_multi_remove_handle($mh, $ch);
curl_multi_close($mh);
}
There were several issues in your original code, which were fixed in the code above.
Each $ch should be an independent handle, not a common one
curl_multi_getcontent should be called on each $ch handle, you are equivalent to only calling the last $ch of each group.
curl_multi_remove_handle should be called on each $ch handle. It is more appropriate to call it after the previous curl_multi_getcontent.
$newData = array_chunk($data, 10, true);
foreach ($newData as $k => $tmp) {
$mh = curl_multi_init();
$chs = [];
foreach ($tmp as $key => $url) {
$ch = curl_init();
$chs[$key] = $ch;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/x-www-form-urlencoded;charset=utf-8;'));
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $tmp);
curl_multi_add_handle($mh, $ch);
}
$active = null;
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc == CURLM_OK) {
if (curl_multi_select($mh) != -1) {
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
foreach ($chs as $key => $ch) {
$res[$k][$key] = curl_multi_getcontent($ch);
curl_multi_remove_handle($mh, $ch);
}
curl_multi_close($mh);
}
var_dump($res);

How to create a table from this ForEach Loop?

I am trying to fetch all the domains I have purchased from Godaddy account using their API. However, It's not showing in a table format for proper view. Can anybody guide me on how to create a table out of this API results? Currently, it's showing results each at a different line but I prefer to view them in a table format.
Thanks!!
<?php
$domains = getDomains();
// check if error code
if(isset($domains['code'])){
$msg = explode(":",$domains['message']);
$msg = '<h2 style="text-align:center;">'.$msg[0].'</h2>';
// proceed if no error
} else {
$msg = '';
foreach ($domains as $domain){
$msg .= $domain['domain'].'</br>';
$msg .= $domain['createdAt'].'<br>';
$domain['createdAt'];
$domain['domain'];
$domain['domainId'];
$domain['expirationProtected'];
$domain['expires'];
$domain['holdRegistrar'];
$domain['locked'];
$domain['nameServers'];
$domain['privacy'];
$domain['renewAuto'];
$domain['renewDeadline'];
$domain['renewable'];
$domain['status'];
$domain['transferProtected'];
}
}
echo $msg;
function getDomains(){
$status = 'ACTIVE';
$limit = '999';
$url = "https://api.godaddy.com/v1/domains?statuses=$status&limit=$limit";
// set your key and secret
$header = array(
'Authorization: #########'
);
//open connection
$ch = curl_init();
$timeout=60;
//set the url and other options for curl
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); // Values: GET, POST, PUT, DELETE, PATCH, UPDATE
//curl_setopt($ch, CURLOPT_POSTFIELDS, $variable);
//curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
//execute call and return response data.
$result = curl_exec($ch);
//close curl connection
curl_close($ch);
// decode the json response
$dn = json_decode($result, true);
return $dn;
}
?>

shorte.st new api convert to php curl

shorte.st is a url shorten service.
recently they changed the api as following:
curl commandline
curl H "public-api-token: fakekey" -X -d "urlToShorten=google.com" PUT http://api.shorte.st/v1/data/url
response:
{"status":"ok","shortenedUrl":"http:\/\/sh.st\/XXXX"}
How to change it into php curl version?
function shst($url){
$apiurl="https://api.shorte.st/v1/data/url";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $apiurl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT' );
curl_setopt($ch, CURLOPT_HTTPHEADER, array('public-api-token: fakekey','X-HTTP-Method-Override: PUT'));
curl_setopt($ch, CURLOPT_POSTFIELDS, "urlToShorten=".$url);
$data = curl_exec($ch);
curl_close($ch);
$obj = json_decode($data);
$ret=$obj->{'shortenedUrl'};
return $ret;
}
The sample they provided used the wrong url. should be https not http
This works:
function shst($url){
$key="";//your key
$curl_url = "https://api.shorte.st/s/".$key."/".$url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$array = json_decode($result);
$shortest = $array->shortenedUrl;
return $shortest;
}

How to upload file via cURL in kohana 3?

I try
$request = Request::factory($url)->method(Request::POST)->post('xml', '#' . $filepath);
echo $request->execute();
but print_r($_FILES); in destination script returns empty array.
Version of Kohana is 3.2.0 stable
What I want is a simple analog of
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('xml' => '#' . $filepath));
$res = curl_exec($ch);
echo $res;
Try:
$request_url = 'your_url_here';
$post_params['name'] = urlencode('Test User');
$post_params['file'] = ‘#’.'demo/testfile.txt’;
$post_params['submit'] = urlencode('submit');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);
$result = curl_exec($ch);
curl_close($ch);
Now it's possible to do that with Kohana 3.3 using Request#files() method.
Edit: actually, it's not part of official release, so before 3.4 you probably won't be able to do it.