PHP curl - been getting spammed with these warnings recently, out of nowhere - php

private function _curl($url, $values)
{
$curl = curl_init($url);
$options = array(
CURLOPT_VERBOSE => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => http_build_query($values),
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_TIMEOUT => 10,
CURLOPT_HTTPHEADER => "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
//CURLOPT_SSL_VERIFYPEER => false //for dev
);
curl_setopt_array($curl, $options);
$rep = curl_exec($curl);
parse_str($rep, $response);
curl_close($curl);
return $response;
}
So that's the code I'm using, and I keep getting these warnings:
PHP Warning: curl_setopt_array(): You must pass either an object or an array with the CURLOPT_HTTPHEADER, CURLOPT_QUOTE, CURLOPT_HTTP200ALIASES and CURLOPT_POSTQUOTE arguments
This code has been working fine for years and is only suddenly causing issues.
I would just pass those parameters, but I'm wondering why this suddenly isn't working, and it used to work correctly? And is there a way to get around this without passing the parameters? I also can't seem to find any documentation anywhere saying these parameters are needed...

The issue was the fact that CURLOPT_HTTPHEADER needs to be an array, as follows:
CURLOPT_HTTPHEADER => Array("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")

Related

Unable to POST data TO REST API Using PHP CURL but it work fine with Postman

I am trying to post a data to REST API using PHP/CURL and seems it's not working, as i get 301 Moved Permanently error,But it works fine with Postman. Here is the snippet of PHP CURL which i generated form Postman
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'api url',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('text' => 'test'),
CURLOPT_HTTPHEADER => array(
'api_key: key',
'Content-Type:application/json',
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
And it always return 301 Moved permanently, Note if i change the API key i got Unauthorised error, which means it hits the server, but i am not sure what i am missing, I have tried with multiple headers combinations.
Any help on this regard would be highly appreciated.
Quoting the PHP manual :
CURLOPT_POSTFIELDS: This parameter can either be passed as a urlencoded
string like 'para1=val1&para2=val2&...' or as an array with the field
name as key and field data as value. If value is an array, the
Content-Type header will be set to multipart/form-data.
Since you use array you'll need to use multipart.
See this post : curl POST format for CURLOPT_POSTFIELDS

How to add attachments to a page using the REST API and PHP?

I'm trying to add an attachment to a page using the REST API and PHP, but I can't get it to work. I got it to work with Postman, no problem, but the PHP code it spits out doesn't seem to be working, and it doesn't even give out an error message.
This is the code I've been using:
<?php
$curl = curl_init();
$restApiUrl = 'http://localhost:8090/rest/api/content/{pageId}/child/attachment/';
$filePath = 'C:/Users/{user}/Desktop/example.png';
$auth = '{auth}';
curl_setopt_array($curl, array(
CURLOPT_URL => $restApiUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('file' => new CURLFILE($filePath)),
CURLOPT_HTTPHEADER => array(
'Content-Type: image/png;charset=UTF-8',
'X-Atlassian-Token: no-check',
'Authorization: Basic ' . $auth
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
The values inside curly brackets (like {auth}) are just placeholders.
Nothing seems to happen with this, no error message, no attachment, nothing... All previous requests I've created in Postman (like creating new pages) usually worked in PHP right away, but not file uploads. Does anyone know what I'm doing wrong here?
Se the content type to:
"Content-Type: multipart/form-data"

Export HTTP POST request with file from Postman not working

I have a Postman HTTP request using POST, a form data field of a file saved under the key plsm_xls_file[].
The file is in the local filesystem.
This request runs perfectly from POSTMAN but when I try to export it to PHP-Curl from the Code Snippets I get something like this:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://mydomain.nl/po_upload3.php?xlsimport=2',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('plsm_xls_file[]'=> new CURLFILE('/C:/Users/myuser/Documents/vita_debug/201216_FG_PC_68715.xlsx'),'template_id' => '170'),
CURLOPT_HTTPHEADER => array(
'cookie: PHPSESSID=509e15pepo3ok80nd74jhdis33;'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
It's not working. It's like the file isn't properly attached to the HTTP request.
EDIT: I finally understood that the problem was that POSTMAN has access to my filesystem but the remote server where I tried to run the exported snippet don't- A very silly mistake on my side.
I had this problem a while back, and while there was never a true resolution (even in PHP bug tracker), I was able to fix it by not including the file in the setopt_array command.
PHP 7.2 CURLFile Gives "Invalid Filename" Warning
In short, try taking your CURLOPT_POSTFIELDS option out of the curl_setopt_array call and add this:
curl_setopt($curl, CURLOPT_POSTFIELDS, array('plsm_xls_file[]'=> new CURLFILE('/C:/Users/myuser/Documents/vita_debug/201216_FG_PC_68715.xlsx'),'template_id' => '170'));

PHP cURL gives an error but works fine in POSTMAN

I have some problem(s) with PHP cURL. I tried to get data from the API using PHP cURL. This is my cURL code in PHP :
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://www.example.com/dos/AW/API",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"Filter\" : {\"IsActive\" : \"True\",\"OutputSelector\" : \"Name\"}}",
CURLOPT_HTTPHEADER => array(
"API_ACTION: GetItem",
"API_KEY: MHlIARzQqxVpOg2dUxH4q9w7bx3pOL6K",
"Accept: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
With that code I can get a response but the response contains some errors. I also tried using POSTMAN to check, and the API works fine as I got a successful response with the same data. My question is: "Is there anything wrong with my cURL code that would explain why I got an error when I used cURL and I got successful response in POSTMAN "?
I would appreciate if someone could help me with this.
Thank you very much.
given that you aren't showing us the successful postman request, we can't know for sure what errors you make, that said, you are making a couple of obvious mistakes here.
first off, when debugging curl code, use CURLOPT_VERBOSE , it gies you a lot of useful information when debugging your curl requests (and if you did this, you would probably notice how the Postman requests's content-type is completely different from curl's content-type http headers - more on this soon)
second, when you want a POST request, don't use CURLOPT_CUSTOMREQUEST, use CURLOPT_POST.
third, when passing a string to CURLOPT_POSTFIELDS, the content-type implicitly becomes Content-Type: application/x-www-urlencoded, unless you override it. and you are obviously NOT sending x-www-urlencoded data, but JSON-encoded data, so your content-type is all wrong, its supposed to be Content-type: application/json
fourth, you can hardcode the json if you want, but the code looks much prettier if you json_encode it
fifth, don't use setopt / setopt_array without checking the return type.
fixing all that, you'll end up with something like:
function ecurl_setopt_array($ch, array $options) {
if (! curl_setopt_array ( $ch, $options )) {
throw new \RuntimeException ( 'curl_setopt_array failed. ' . curl_errno ( $ch ) . ': ' . curl_error ( $ch ) );
}
}
$curl = curl_init ();
ecurl_setopt_array ( $curl, array (
CURLOPT_URL => "https://www.example.com/dos/AW/API",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_VERBOSE => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode ( array (
'Filter' => array (
'IsActive' => 'True',
'OutputSelector' => 'Name'
)
) ),
CURLOPT_HTTPHEADER => array (
"API_ACTION: GetItem",
"API_KEY: MHlIARzQqxVpOg2dUxH4q9w7bx3pOL6K",
"Accept: application/json",
'Content-Type: application/json'
)
) );
$response = curl_exec ( $curl );
$err = curl_error ( $curl );
curl_close ( $curl );
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Edit: fixed the json data, when i wrote it, i didn't see that isActive is not an actual boolean, but the string literal True - i mistakenly encoded it as a json boolean true instead, sorry, fixed. (although i suspect it's supposed to be a boolean anyway, and that your original code just encodes it wrong, perhaps you should double check isActive's type in the api docs, assuming there is one)
#Antonio, Response you are getting is from the other end, might be you are missing something which restrict the processing of query at other end. try to print http_code, or use curl_getinfo to get complete information.
in case of response code is 200, then you may ask from another end to validate the request.
PS: not able to comment because of repo restrictions.

PHP cURL Json Post - doing something wrong

I'm new to php and I'm trying to create a simple example for calling our company api. I got NetBeans IDE 7.1.2 to work last night (yay) but I cannot seem to get the following code to show me anything. I can run it in the debugger. I can step through it. I even get to the end without errors, but the curl_exec returns just 0. I have added the CURLOPT_PROXYPORT so that I can get fiddler to see the traffic, but fiddler sees nothing. I am also trying to run this as a php command line (if that has any bearing).
I know I'm doing something stupid... but that's the problem with stupid.
<?php
$url = 'https://target.boomerang.com/api/JobCreate';
$authToken = 'phptest';
$data = array(
"emailHTML" => "Howdy",
"jobKind" => "email",
"senderEmail" => "bob#boomerang.com",
"subject" => "My howdy email"
);
$data_string = json_encode($data);
$headers = array(
'Content-type: application/json',
'auth_token: ' . $authToken,
'Accept: application/json',
'Expect:'
);
$ch = curl_init();
$args = array(
CURLOPT_URL => $url,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $data_string,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_PROXYPORT => "localhost:8888"
);
curl_setopt_array($ch, $args);
$res = curl_exec($ch);
$res_data = json_decode($res, true);
print($res_data);
?>
Thanks for any help.
So the problem was caused by SSL certificate mismatch? I suppose it can be solved by adding this...
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
...into $args array.

Categories