I've done every bit of tips and answers but I can't seem to find the right solution.
my cURL is as follows
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $curlIP."api/sparts/search/");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, "key=".$q."&page=".$pg);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($curl);
curl_close ($curl);
What does this do?
It gets products data from a server where $q is the search key and $pg is the page number
This cURL works as intended assuming the server will always have an internet connection. As the developer we needed to have an error message whenever the cURL cannot connect to the server.
What is the problem?
For me to actually set an error message I need to know first if the error has happened. This where it gets weird for me. I tried to simulate connection failure by disabling the internet adapter of the server.
The result is:
the page stops parsing everything after the $data = curl_exec($curl);This includes the footer and everything after that line. I knew it because I tried to put echoes before and after curl_exec and it really stops there. therefore I cannot catch the error itself.
What have I tried?
I tried
PHP curl_error() - which didn't really help since the script stops at curl_exec
PHP curl_errno() - same
try and catch - This should have catch the error but it didn't which frustrates me even more.
Related
I am trying to use curl and php to send a file from my liquid web server to the bullhorn vms system. I have tried multiple ways to get this to work but the current code i am using is:
$url_path_str = 'https://rest91.bullhornstaffing.com/rest-services/(corp token)/file/Candidate/'.$entityid.'/raw?filetype=SAMPLE&externalID=portfolio?BhRestToken='.$bhresttoken;
$file_path_str = '/home/path/'.$row["first_name"].$row["last_name"].'.pdf';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ''.$url_path_str.'');
curl_setopt($ch, CURLOPT_PUT, 1);
$fh_res = fopen($file_path_str, 'r');
curl_setopt($ch, CURLOPT_INFILE, $fh_res);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file_path_str));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curl_response_res = curl_exec ($ch);
echo $url_path_str;
echo $curl_response_res;
curl_close($ch);
fclose($fh_res);
The $entityid, $bhresttoken, $row["first_name"], and $row["last_name"] are variables I have created and using echo shows that they are working. Two errors show up separately when I try to execute this code.
The first error is
"{"errorMessage":"Missing 'BhRestToken' in request header or parameter.","errorMessageKey":"errors.authentication.missingRestToken","errorCode":412}"
which is weird because the bhresttoken allegedly isn't required but also it is definitely showing correctly at the end of the "url_path_str", so is it in the wrong spot?
The other issue that I get is
"An error occurred while processing your request. Reference #179.24343217.1582146766.1f0b67".
From what I've been told the reference number is a generic issue so I'm guessing that my code is flawed somewhere however I cannot figure out what is wrong.
Any advice or input would be much appreciated. Thank you in advance.
So I'm rather new to using cURL in PHP, I was told to use it for a task this week and it's been nothing but a pain, and I can't seem to find a solution to my problem no matter how hard I search.
What I am attempting to do is send a file to an upload directory on my hosted server from a remote portal manager that I have built. The file uploadhandler in the portal manager connects via curl to the remote destination and then the remote destination grabs the info and processes the file like normal. No matter what I've been trying though everything just throws back a failed response.
Here is the updated version of the code I am working with
updates
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, false);
//curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt(
$ch,
CURLOPT_POSTFIELDS,
array(
'file' =>
'#' .$_FILES['doc']['tmp_name'][$i]
.';filename=' .$_FILES['doc']['name'][$i]
.';type=' .$_FILES['doc']['type'][$i]
)
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//$response = curl_exec($ch);
if(curl_exec($ch) === false){
echo curl_error($ch);
echo curl_errno($ch);
}else{
echo "ok";
}
With all of this information set I am getting no value for curl_error but I get a value of 43 for curl_errno.
From what I have been researching, error 43 for curl is
CURLE_BAD_FUNCTION_ARGUMENT (43)
Internal error. A function was called with a bad parameter.
However all my functions for the curl_setopt() are put together correctly based on the info from php.net. So this is where I am now confused, because I have no idea what is causing this to happen. Thanks again for the help!
I'm using an URL to transmit parameters to a remote location.
I need to call the URL using PHP - without redirecting to the URL.
I've tried different solutions without luck. I am no way near an expert when dealing with PHP, I am even unsure if I am wording the question correctly.
I've tried:
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, "https://remotelocation.com/storedata.aspx?name=".$name."&email=".$email_address.);
curl_exec ($curl);
curl_close ($curl);
and:
file_get_contents("https://remotelocation.com/storedata.aspx?name=".$name."&email=".$email_address.")
without any luck.
Calling the URL manually in a browser (using definite values instead of variables) the data is stored. Using mail() instead the data is also transmitted.
I know the solution must be pretty simple. Can anyone help to point me in the right direction?
Not sure why it didn't work with file_get_contents. But for curl you need to add the following few options as well(There are a lot in fact, you can check it from here).
// to handle https links
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
// return response
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
And to grab the returned response html
$response_html = curl_exec ($curl);
submitting xml data using post method but its giving following error
ASP 500 Error
An error occurred processing the page you requested.
Please see the details below for more information.
COM Error Number -2146827864 (0x800A01A8)
File Name /vm/VM_jeune/includes/partenaires_calcul_primes.asp
Line Number 110
Brief Description Object required: 'objDOMXPCML.selectSingleNode(...)'
<?php
/*
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/
$xml_data ='<aatavailreq1>'.
'<agency>'.
'<iata>1234567890</iata>'.
'<agent>lgsoftwares</agent>'.
'<password>mypassword</password>'.
'<brand>phpmind.com</brand>'.
'</agency>'.
'<passengers>'.
'<adult age="" id="1"></adult>'.
'<adult age="" id="2"></adult>'.
'</passengers>'.
'<hotelavailreq1>'.
'<destcode>JHM</destcode>'.
'<hotelcode>OGGSHE</hotelcode>'.
'<checkindate>101009</checkindate>'.
'<checkoutdate>101509</checkoutdate>'.
'<usefield>1</usefield>'.
'</hotelavailreq1>'.
'</aatavailreq1>';
$URL = "http://www.example.com/vm/makessense/calcul_primes.asp";
$ch = curl_init($URL);
// curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_URL, $URL );
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
print_r($output);
?>
its giving 500 error not accepting any data please tell me what is the error in this code.
An error 500 means that the program receiving the XML file has crashed. We can see that it has a bug because it isn't handling the error condition gracefully, but we can't see what that bug is, nor how it is affected by your input data because the ASP crash message has insufficient information to work it out.
So the basic answer to your question is: We don't know, and we can't tell. The only people who could tell you what the problem is would be the owners of the site that has crashed.
You could use a tool like Fiddler to examine the HTTP traffic. This will help you to prove that your code is sending the XML string correctly. You could also use an XML validation tool to prove that the XML is valid.
But if those tests prove that it's okay at your end (and I suspect they will), then there's nothing much else you can do other than ask the people receiving the XML what's wrong with their software and how you can resolve the problem; it may need a code fix on their side, or they may be able to help you work around the issue. Either way, there's nothing we can do here to help you with it.
I've been through hell and high water with this problem. I get a 500 error on a page a tiny, tiny fraction of the time. I have been completely unable to reproduce it, but Google insists that they see a 500 code. Fetch as Googlebot says it's successful, however something is wrong. I've been down many avenues and the only recourse I have left is to brute-force the local copy of the website.
I want to use curl to hammer the dev site until I get a 500 error, and when I do, to output the body of the page to the terminal so I can actually get some useful information.
for(;;){
$url = "http://www.blahblah.dev/";
$ch = curl_init();
//Set the URL
curl_setopt($ch, CURLOPT_URL, $url);
//Enable curl response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//Enable POST data
curl_setopt($ch, CURLOPT_POST, true);
//Use the $pData array as the POST data
curl_setopt($ch, CURLOPT_POSTFIELDS, $jData);
$result = curl_exec($ch);
if(strstr($result, "error")){
echo $result;
exit();
}
curl_close($ch);
usleep(500000);
}
As you can see, I'm simply checking to see if "error" appears in the body, as I can't figure out how to check for a 500 error properly. I realize that this is a terrible and contrived way of debugging, but it's all I've got at this point. Thanks!