How can i send multi url using Curl in PHP - php

I have a multi url to send to other server using CURL
I want each link to send using GET method get me some value in response header
So i'm trying this solution with curl-multi-init.php
but only the first curl is executed when I put it after first URL and other appropriate.
But when I deleted the result is
Warning: Undefined array key 11 in //path on line 41
Deprecated: explode(): Passing null to parameter #2 ($string) of type
string is deprecated in //path on line 41
Warning: Undefined array key 1 in //path on line 44
Warning: Undefined array key 1 in //path on line 46
0
Warning: Undefined array
key 1 in //path on line 55
<?php
$ch1 = curl_init();
$header1 = [
'Content-Type: application/json',
'Accept: application/json,version=2',
'Accept-Language: en-US,en-SA;q=0.9'
];
$response_headers = [];
$header_callback = function($ch1,$header1) use (&$response_headers){
$len = strlen($header1);
$response_headers[] = $header1;
return $len;
};
// set first URL and other appropriate
curl_setopt($ch1, CURLOPT_URL, "http://127.0.0.1/v1/rest/auth");
curl_setopt($ch1, CURLOPT_HEADER, true);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch1, CURLOPT_HTTPHEADER, $header1);
curl_setopt($ch1, CURLOPT_HEADERFUNCTION, $header_callback);
curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, 'GET');
$response1 = curl_exec($ch1);
$part = explode(":",$response_headers[11]);
echo $part[1];
$auth = ( (doubleval(doubleval ($part[1]) / 13) % 99999) * 17 );
echo number_format($auth, 0, '.', '');
curl_close($ch1);
echo $response1;
$ch2 = curl_init();
$header2 = [
'Content-Type: application/json',
'Accept: application/json,version=2',
'Auth:'.$part[1],
'Aut:'.$auth,
'Accept-Language: en-US,en-SA;q=0.9'
];
$response_headers2 = [];
$header_callback2 = function($ch2,$header2) use (&$response_headers2){
$len2 = strlen($header2);
$response_headers2[] = $header2;
return $len2;
};
curl_setopt($ch2, CURLOPT_URL, "http://127.0.0.1/v1/rest/auth?usr=root&pwd=root");
curl_setopt($ch2, CURLOPT_HEADER, true);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_HTTPHEADER, $header2);
curl_setopt($ch2, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch2, CURLOPT_HEADERFUNCTION, $header_callback2);
$response2 = curl_exec($ch2);
$session = explode(":",$response_headers2[1]);
echo $session[1];
curl_close($ch2);
echo $response2;
var_dump($ch2); //result of var_dump($ch2) //object(CurlHandle)#3 (0) { }
$ch3 = curl_init();
$header3 = [
'Content-Type: application/json',
'Accept: application/json,version=2',
'Session:'.$session[1],
'Accept-Language: en-US,en-SA;q=0.9'
];
$response_headers3 = [];
$header_callback3 = function($ch3,$header3) use (&$response_headers3){
$len3 = strlen($header3);
$response_headers3[] = $header3;
return $len3;
};
curl_setopt($ch3, CURLOPT_URL, "http://127.0.0.1/api/example1");
curl_setopt($ch3, CURLOPT_HEADER, 0);
curl_setopt($ch3, CURLOPT_HEADER, true);
curl_setopt($ch3, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch3, CURLOPT_HTTPHEADER, $header3);
curl_setopt($ch3, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch3, CURLOPT_HEADERFUNCTION, $header_callback2);
$response3 = curl_exec($ch3);
curl_close($ch3);
echo $response3;
$ch4 = curl_init();
curl_setopt($ch4, CURLOPT_URL, "http://127.0.0.1/api/example2");
curl_setopt($ch4, CURLOPT_HEADER, true);
curl_setopt($ch4, CURLOPT_CUSTOMREQUEST, 'GET');
$response4 = curl_exec($ch4);
curl_close($ch4);
echo $response4;
?>
So how can i use multi curl to get value from each response header ?

Related

Using Curl with PHP Command line to PHP

This works from the command line how do I convert this to PHP I have tried anything but nothing seems to work.
curl -H "Authorization: Bearer APIKEY" https://www.gocanvas.com/apiv2/forms.xml -F 'username=XXXXXXX#XXXXXXXXXX.com'
Here is a function I have tried.
function getgocan(){
$url = 'https://www.gocanvas.com/apiv2/forms.xml?username=XXXXX#XXXXXXXXXX.com';
$ch = curl_init();
$jsonData = array(
'text' => ''
);
$jsonDataEncoded = json_encode($jsonData, true);
$header = array();
$header[] = "APIKEY";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
}
-F sends form fields in the POST data, not URL query parameters. So you need to put username=XXX into CURLOPT_POSTFIELDS, and it shouldn't be JSON.
You can give an associative array to CURLOPT_POSTFIELDS, and it will encode it automatically.
function getgocan(){
$url = 'https://www.gocanvas.com/apiv2/forms.xml';
$ch = curl_init();
$params = array(
'username' => 'username=XXXXX#XXXXXXXXXX.com'
);
$header = array(
'Authorization: Bearer APIKEY'
);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
curl_close($ch);
var_dump($$result);
}

Issue with Strava API

I am trying to get a list of my activities using cURL I can get the list of my clubs ok. Can't seem to figure out what I am doing wrong
I am having an issue when using the request. $ http GET "https://www.strava.com/api/v3/activities/{id}?include_all_efforts=" "Authorization: Bearer [[token]]"
I cam getting an error
string(95) "{"message":"Record Not Found","errors":
[{"resource":"Activity","field":"id","code":"invalid"}]}"
**I have tried the following**
$num = "2149016538";
$int = (int)$num;
// $url = 'https://www.strava.com/api/v3/athlete/clubs';
$url = 'https://www.strava.com/api/v3/activities/{'.$int.'}?include_all_efforts=true';
**No success**
**I have also tried this**
$num = "2149016538";
$int = (int)$num;
// $url = 'https://www.strava.com/api/v3/athlete/clubs';
$url = 'https://www.strava.com/api/v3/activities/?id='.$int.'?include_all_efforts=true';
$authorization = "Authorization: Bearer 23232323";
$num = "2149016538";
$int = (int)$num;
// $url = 'https://www.strava.com/api/v3/athlete/clubs';
$url = 'https://www.strava.com/api/v3/activities/{'.$int.'}?include_all_efforts=true';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
var_dump($result);
curl_close($ch);
json_decode($result);

Zoho API-V2 Add Attactmetn URL

I am trying to add attachment url in crm. I am flowing this documentation . But i got error !
This is my code :
$zoho_url = "https://www.zohoapis.com/crm/v2/$module/$id/Attachments";
$post['attachmentUrl'] = $url;
$ch=curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_URL,$zoho_url);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
$headers = array();
$headers[] = "Authorization: ".$authtoken;
$headers[] = "Content-Type: multipart/form-data";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$err = curl_errno($ch);
curl_close ($ch);
if ($err) {
$result = $err;
} else {
$result = $response;
}
print_r($result);
This is response :
{"code":"INVALID_REQUEST","details":{},"message":"unable to process your request. please verify whether you have entered proper method name, parameter and parameter values.","status":"error"}
I made this code for attach a file in a zoho record.
//Get the oauth Token
$accessToken=getCurrentAccessToken();
// files to upload
$tmpfile;
$filename;
$type;
foreach($files as $file){
$tmpfile = $file['tmp_name'];
$filename = basename($file['name']);
$type = $file['type'];
}
$cfile = new CURLFile(realpath($tmpfile),$type,$filename);
$post_data = array (
'file' => $cfile
);
$url = "https://www.zohoapis.com/crm/v2/$module/$id/Attachments";
$headers = array(
'Content-Type: multipart/form-data',
sprintf('Authorization: Zoho-oauthtoken %s', $accessToken)
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$response = curl_exec($ch);
return $response;

php curl post json data contains a null entry for parameter

i've create a php function to make a post json data request using curl
My Code :
$url = 'http://77.42.154.142:90/home/test/GetNewCustomer';
$data = array("UpdatedOn" => "1/23/2004 1:00AM","CreatedOn" => "1/23/2015 1:00AM");
$fields = json_encode($data);
$post = curl_init();
curl_setopt($post, CURLOPT_URL, $url);
curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'sduser: 34EE0A61-F3A5-4E21-86D3-C7B7DAF9C8F9913982A9-3025-4C37-8093-B664B9310F91',
'Content-Length:'.strlen($fields)
));
curl_setopt($post, CURLOPT_POST, 1);
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml; charset=utf-8', 'Content-Length: '.strlen($fields)));
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
//curl_setopt($post, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($post);
// for debugging
var_dump(curl_getinfo($post), $result);
// if(false === $result) { // request failed
// die('Error: "' . curl_error($post) . '" - Code: ' . curl_errno($post));
// }
curl_close($post);
But i've got this response The parameters dictionary contains a null entry for parameter
Any Suggestion ?

POST file Curl Commandline to PHP Curl

We are trying to convert use the following curl cmdline which is working fine
curl --request POST --data-binary #"/home/project/enrol/my.wav" --header "Content-Type:audio/wav" --header "VsitEmail: 200037test2#test.com" --header "VsitPassword: 8270f2824111e04d9278c01a92b388147d9d02e0b50d946d25d00db375ff1282" --header "VsitDeveloperId: 200037" https://siv.voiceprintportal.com/sivservice/api/enrollments
to PHP libcurl which is constantly giving internal error.
Please suggest where i am wrong .
Code
$ch = curl_init();
$path="/home/project/enrol/my.wav";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: audio/wav","VsitEmail: 200037test2#test.com","VsitPassword: 8270f2824111e04d9278c01a92b388147d9d02e0b50d946d25d00db375ff1282","VsitDeveloperId: 200037"));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt( $ch , CURLOPT_VERBOSE , 0 );
curl_setopt($ch, CURLOPT_POSTFIELDS,array('file' => '#'.$path));
$result = curl_exec($ch);
//close connection
curl_close($ch);
print_r($result);
Update 1
$localfile='/home/project/enrol/my.wav';
$url = 'https://siv.voiceprintportal.com/sivservice/api/enrollments';
$ch = curl_init();
$fields = array('file' => '#' .$localfile);
$resource = curl_init();
curl_setopt($resource, CURLOPT_URL, $url);
//curl_setopt($resource, CURLOPT_HTTPHEADER, $header);
curl_setopt($resource, CURLOPT_HTTPHEADER, array("Content-Type:
audio/wav","VsitEmail: 200037test2#test.com","VsitPassword: 8270f2824111e04d9278c01a92b388147d9d02e0b50d946d25d00db375ff1282","VsitDeveloperId: 200037"));
curl_setopt($resource, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($resource, CURLOPT_POST, 1);
curl_setopt($resource, CURLOPT_BINARYTRANSFER, true);
curl_setopt($resource, CURLOPT_POSTFIELDS, $fields);
//$result = json_decode(curl_exec($resource));
$result=curl_exec($resource);
curl_close($resource);
echo "The result is";
print_r($result);
Check the following code
$url = 'http://google.com';
$header = array('Content-Type: multipart/form-data');
$fields = array('file' => '#' . $_FILES['file']['tmp_name'][0]);
$resource = curl_init();
curl_setopt($resource, CURLOPT_URL, $url);
curl_setopt($resource, CURLOPT_HTTPHEADER, $header);
curl_setopt($resource, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($resource, CURLOPT_POST, 1);
curl_setopt($resource, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($resource);
curl_close($resource);
It got resolved by passing the raw data file instead of an array. Correct code is
$localfile='#/home/project/enrol/my.wav';
$url = 'https://siv.voiceprintportal.com/sivservice/api/enrollments';
$data = file_get_contents('/home/project/enrol/my.wav');
$headers = array();
$headers[] = 'X-Requested-With: JSONHttpRequest';
$headers[] = 'Content-Type: audio/wav';
$headers[] = 'VsitEmail: 200037test2#test.com';
$headers[] = 'VsitPassword: 8270f2824111e04d9278c01a92b388147d9d02e0b50d946d25d00db375ff1282';
$headers[] = 'VsitDeveloperId: 200037';
$resource = curl_init();
curl_setopt($resource, CURLOPT_URL, $url);
curl_setopt($resource, CURLOPT_HTTPHEADER,$headers);
curl_setopt($resource, CURLOPT_POST,1);
curl_setopt($resource, CURLOPT_BINARYTRANSFER, true);
curl_setopt($resource, CURLOPT_POSTFIELDS,$data);
$result = json_decode(curl_exec($resource));
curl_close($resource);
echo "The result is";
print_r($result);

Categories